Alan B. Shepard Jr., the first American to fly in space, lifting national spirits at a decisive moment of cold war anxiety, and later one of only 12 astronauts to walk on the Moon, died late Tuesday night at a hospital in Monterey, Calif. He was 74 and lived in Houston and Pebble Beach, Calif. The family would not disclose the cause of death, saying only that it came after a long illness. But another former astronaut, Senator John Glenn of Ohio, said Mr. Shepard had been suffering from leukemia. On the morning of May 5, 1961, Mr. Shepard became an immediate American hero. A lean, crew-cut former Navy test pilot, then 37, he began the day lying on his back in a cramped Mercury capsule atop a seven-story Redstone rocket filled with explosive fuel. After four tense hours of weather and mechanical delays, he was shot into the sky on a 15-minute flight that grazed the fringes of space, at an altitude of 115 miles, and ended in a splashdown in the Atlantic Ocean 302 miles downrange from Cape Canaveral, Fla. Though not much by today's standards, the brief suborbital flight had stopped a whole country in its tracks, waiting anxiously at radios and television sets. When the message of success came through -- with a phrase that would enter the idiom, ''Everything is A-O.K.!'' -- everyone seemed to let out a collective sigh of relief. Yuri Gagarin of the Soviet Union may have been first into space, 23 days before, and have flown a full orbit, but with Mr. Shepard's flight the United States finally had reason to cheer. In fact, Mr. Shepard's success is credited with giving President John F. Kennedy the confidence to commit the nation to the goal of landing men on the Moon within the decade. He announced the goal to Congress on May 25. Daniel S. Goldin, head of the National Aeronautics and Space Administration, said the Shepard flight was ''a tremendous statement about tenacity, courage and brilliance,'' adding with a tone of lingering awe: ''He crawled on top of that rocket that had never before flown into space with a person aboard and he did it. That was an unbelievable act of courage.'' Dr. John M. Logsdon, a political scientist at George Washington University and author of ''The Decision to Go to the Moon,'' said, ''One can make the argument that the success of the Shepard flight enabled the decision to go to the Moon.'' The historian Arthur M. Schlesinger Jr., a member of the Kennedy White House staff, recalled that the Gagarin flight and then the Bay of Pigs fiasco in Cuba a few days later had given ''Americans a slight inferiority complex.'' Then the Shepard flight produced ''a great exhilaration and renewed our confidence in our capacity as a nation.'' President Clinton hailed Mr. Shepard as ''one of the great heroes of modern America.'' Ten years after the first flight, Mr. Shepard made a comeback from a serious inner-ear problem, called Meuniere's syndrome, that had grounded him. And it was also a comeback flight for the space program. On Jan. 31, 1971, he took off for the Moon in Apollo 14, the first lunar mission since the near-disaster of Apollo 13 the year before. He and Edgar Mitchell spent 33 hours on the Moon, a visit made memorable by Mr. Shepard's taking several spirited whacks at golf balls with a makeshift six-iron he had taken along. In the weak lunar gravity, the balls sailed. ''He lived every golfer's dream,'' Mr. Clinton remarked, ''taking a six iron and hitting the ball, in his words, 'for miles and miles.' '' Mr. Shepard was one of the original seven Mercury astronauts chosen in 1959 by the fledgling space agency and introduced with great fanfare. His death leaves four survivors: Scott Carpenter, L. Gordon Cooper, Walter Schirra and Senator Glenn, who at 77 is training for a kind of farewell flight on the space shuttle in October. Mr. Glenn in 1962 became the first American to orbit the earth. Virgil I. (Gus) Grissom died in a cockpit fire during a launching-pad training exercise in 1967. Donald K. (Deke) Slayton died of cancer in 1993. In 1974, Mr. Shepard retired from both NASA and the Navy, where he had achieved the rank of rear admiral. Already wealthy from real estate and other investments, he became chairman of the Marathon Construction Corporation in Houston and later founded his own business company, Seven Fourteen Enterprises. The name recognized his two missions, Freedom 7 and Apollo 14. Alan Bartlett Shepard Jr. was born on Nov. 18, 1923, in East Derry, N.H., where his father, a retired Army colonel, was in the insurance business. Young Alan took odd jobs at a local airfield to learn more about planes. His education began in a one-room country school, continued with high school at the Pinkerton Academy in nearby Derry and was completed at the United States Naval Academy, where he graduated in 1944. He served briefly on a destroyer in the Pacific in World War II before turning to aviation. After flight training in Texas and Florida and service on aircraft carriers in the Mediterranean, he entered test-pilot training at Patuxent River, Md., elevating himself into the elite of military aviation. When NASA asked 110 test pilots to volunteer to be astronauts, Mr. Shepard made the list and was one of seven chosen. Recalling the early days in the astronaut corps, Senator Glenn said yesterday: ''That group was tossed into a maelstrom of activity, attention, travel and training in preparation for those early Mercury flights. It became obvious to all of us very early in our training that Al was a highly intelligent, dedicated leader whose high motivation toward accomplishing our mission was a true inspiration to all.'' The seven astronauts were highly competitive, each vying to be first to fly in space, and Mr. Shepard often seemed to be the most determined and cockiest of the group. As the author Tom Wolfe wrote years later in ''The Right Stuff,'' (Farrar, Straus, Giroux, 1979) Mr. Shepard presented two different faces to his colleagues, ''both the Icy Commander and Smilin' Al,'' but he ''set a standard of coolness and competence that would be hard to top.'' Mr. Shepard made no secret of his intense dislike of Mr. Wolfe's book, particularly some of its character studies and its concentration on some of the astronauts' after-hours pursuits. On a tour promoting his own book, ''Moon Shot,'' (Turner Publishing) written with Mr. Slayton, Mr. Shepard often said, ''We wanted to call ours 'The Real Stuff,' since his was just fiction.'' Mr. Shepard was among three finalists for the first flight, the others being Mr. Grissom and Mr. Glenn. By May 2, when the decision was announced, Mr. Shepard knew he would be only the first American in space, not the first ever. American space managers had scheduled a piloted Mercury flight in March, but problems with a test in January troubled the engineers, who decided on another unmanned test. On April 12, the Russians continued their string of space triumphs by putting Major Gagarin in space for a full orbit. The historian Dr. Walter A. McDougall, in his book ''The Heavens and the Earth: A Political History of the Space Age,'' wrote that ''Gagarin's flight was a second Sputnik'' and when Mr. Shepard's flight three weeks later lasted only 15 minutes on a suborbital trajectory, ''The space gap, in the eyes of the world, had widened.'' The Soviet press agency Tass sneered that the Freedom 7 flight ''cannot be compared with the flight of the Soviet space ship Vostok.'' In the power rivalry of the cold war, the space race had become a kind of moral equivalent of war, and in those early years after the first Sputnik satellite in October 1957, the Soviet space forces seemed to be winning hands down. The United States felt its prestige ebbing with each Soviet launch. Dr. McDougall noted that a poll taken after the Shepard flight revealed that Western Europeans believed the Soviet Union to be ahead in total military strength by 41 percent to 19 percent, and in overall scientific achievement by 39 percent to 31 percent. On that May 5th morning, these things were not on Mr. Shepard's mind. Because of the many delays, he had an unremitting urge to urinate, but no way to get relief in his cocoon of a silver space suit. He kept pleading with the controllers for a break. They would not relent. Mr. Shepard described the experience in ''Moon Shot.'' ''You heard me,'' he radioed to the controllers. ''I've got to pee. I've been in here forever. The gantry is still right here, so why don't you guys let me out of here for a quick stretch?'' ''No way, Alan,'' Gordon Cooper replied, after a consultation with Dr. Wernher von Braun, the rocket scientist. ''Wernher says no.'' Finally, Mr. Shepard suggested that he be allowed to urinate in his suit. But that might short-circuit the medical sensors. ''Tell 'em to turn the power off,'' the desperate astronaut ordered. They did, and relief finally came to Mr. Shepard, not long before he made history. In a decision by President Kennedy, which contrasted with the Soviet policy of secrecy, the launch was broadcast live on radio and television. The capsule separated smoothly from the rocket. A few minutes into the mission, Mr. Shepard took over manual control from the autopilot, putting the craft through some prescribed maneuvers. For five minutes, at maximum altitude and traveling more than 5,000 miles an hour, the astronaut experienced the weightlessness of space. ''Boy, what a ride!'' he said later. Yet, he said, the trip was ''pleasant and relaxing,'' with no effect on his movements and efficiency. The flight was almost flawless, and so was the splashdown in the Atlantic. ''On the way to the carrier I felt relieved and happy,'' the astronaut said later. ''I knew I had done a pretty good job.'' Whether or not he used the expression A-O.K. is problematic. Years later, a reporter asked Mr. Shepard if he had indeed said A-O.K., and where did the term come from. ''Ask Shorty Powers,'' he said enigmatically, referring to Col. John (Shorty) Powers, who was press officer to the first astronauts. It was Colonel Powers who had passed on the quote to reporters. Three days later, Mr. Shepard and his wife, the former Louise Brewer, were entertained by the Kennedys at the White House, then whisked to New York for a ticker-tape parade in the old Lindbergh tradition. Behind the scenes, space, military and budget officials were meeting to draw up recommendations for taking bold steps to catch and surpass Soviet space achievements. A memorandum, handed to President Kennedy the day of Mr. Shepard's White House visit, concluded, ''Of all the programs planned, perhaps the greatest unsurpassed prestige will accrue to the nation which first sends a man to the moon and returns him safely to earth.'' On May 25, Mr. Kennedy told Congress, ''This nation should commit itself to achieving the goal, before this decade is out, of landing a man on the moon and returning him safely to earth. No single space project in this period will be more impressive to mankind or more important for the long-range exploration of space. And none will be so difficult or expensive to accomplish.'' Dr. Logsdon of George Washington University said it was doubtful that Mr. Kennedy would have issued such a challenge, if the Shepard flight had failed. At a dinner celebrating the 30th anniversary of the flight, Mr. Glenn said: ''Al's flight and my flight -- it was almost like we turned a corner. There was a feeling we were coming back. There was a great outpouring of public spirit.'' Mr. Shepard called it ''just the first baby step, aiming for bigger and better things.'' His words would be echoed eight years later by Neil A. Armstrong as he stepped on the Moon with the declaration, ''One small step for man, one giant leap for mankind.'' Mr. Shepard almost did not make it to the Moon himself. Before he could train for a second space flight in the Gemini Project, he developed inner-ear problems. Thomas P. Stafford, a retired astronaut, recalled yesterday that he and Mr. Shepard had been chosen for the first Gemini mission. ''It was a huge blow in his life,'' Mr. Stafford said of the medical problems that grounded Mr. Shepard. ''He was anxious to get back into space, to ride again.'' Instead, for nearly a decade, Mr. Shepard was firmly rooted to NASA managerial jobs, mainly directing the astronaut corps. Mr. Stafford recalled him as a natural leader, who operated ''with more stick than carrot and would lay down the law, saying here's how it's going to be, guys.'' After surgery to correct the ear problem in 1968, Mr. Shepard was cleared to fly again and was assigned to what turned out to be the third lunar landing. Although his first flight had been only 15 minutes, the Apollo 14 journey to the Moon added 216 hours to his log book and rounded out an astronaut career that also included a Congressional Space Medal of Honor. In his last years, he still had time for work promoting the space program and had joined the other surviving Mercury astronauts, and Betty Grissom, the widow of Gus Grissom, in founding the Mercury Seven Foundation, based in Titusville, Fla., near Cape Canaveral. The foundation raises money for scholarships for science and engineering students in college. He was its president and chairman until last October. Besides his wife of 53 years, the survivors include two daughters, Laura Snyder and Juliana Coleman Jenkins, and six grandchildren. Funeral arrangements are pending.