Those who depart in a caravan in the desert, and it is likely that it may lead to the desecration of Shabbat, whether a Torah-level desecration or a rabbinic-level desecration, and the desecration is due to danger—for example, that the caravan will travel on Shabbat more than the Shabbat techum and he will be afraid to remain alone because of bandits or wild animals, or even if he fears for the theft of his animal, his money, and his clothes, for if he travels on foot or naked he will be in danger, as all these things when they occur, it is permitted to desecrate Shabbat for this, for this is truly pikuach nefesh—nevertheless, this is not a certainty. Therefore, his law is like that of a ship: from Wednesday onwards, it is forbidden; and prior to that, it is permitted; and if afterwards a danger occurs to him, he is permitted to desecrate Shabbat. And for the purpose of a mitzvah, such as one ascending to Eretz Yisrael to dwell therein—and even if his intention is to return, it is also a mitzvah to be for some time in Eretz Yisrael—and if a caravan became available to him, even on Erev Shabbat he may set out with them. And the Israelites speak and stipulate with the leader of the group that they shall stand still on Shabbat, and for the caravan this is not a loss, for it is necessary for them to rest one day from the toil of the way. And if afterwards they did not obey and they travel on Shabbat, he must necessarily go with them because of the danger. And when they enter a city on Shabbat, they are permitted to walk throughout the entire city. For even though one who departs outside the techum has only four amot, this applies to one who departs willfully; but for the purpose of a mitzvah, he has two thousand amot in every direction, as has been written in siman 407. And even if they left him outside the city and he wants to enter, since he went out for the purpose of a mitzvah, he has two thousand amot in every direction. And the same law applies even for a voluntary matter when he departed permissibly, which is on Sunday, Monday, and Tuesday—the law is also thus, as has been written there. And so too if he was acting inadvertently and thought it was permitted to depart on Wednesday and Thursday, it is also permitted, for one who says "it is permitted" is considered inadvertent. (Magen Avraham, subsection 18).
היוצאים בשיירא במדבר וקרוב הדבר שאפשר לבא לידי חילול שבת, בין חילול דאורייתא בין חילול דרבנן, והחילול הוא מפני הסכנה, כגון שהשיירא תלך בשבת יותר מתחום שבת והוא יתיירא להשאר לבדו מפני ליסטים או חיות, או אפילו שחושש לגזל בהמתו וממונו ובגדיו, שכשילך רגלי או ערום יהיה בסכנה, דכל הדברים האלו כשאירע - מותר לחלל שבת על זה, דזהו ממש פקוח נפש, ועם כל זה אין זה וודאית. ולכן דינו כמו בספינה, שמן יום ד' - אסור, וקודם לכן - מותר, ואם אחר כך יארע לו סכנה - מותר לו לחלל שבת. ולדבר מצוה, כגון העולה לארץ ישראל לישב בה, ואפילו אם דעתו לחזור - גם כן הוי מצוה להיות קצת זמן בארץ ישראל. ואם נזדמנה לו שיירא - אפילו בערב שבת יכול להפליג עמהן, והישראלים מדברים ופוסקים עם ראש החבורה שיעמדו בשבת, ולהשיירא אין זה הפסד, כי בהכרח להם לנוח יום אחד מעמל הדרך. ואם אחר כך לא צייתו והולכים בשבת - בהכרח לילך עמהם, מפני הסכנה. וכשיכנסו לעיר בשבת - מותרים להלך את כל העיר. דאף על גב דהיוצא חוץ לתחום אין לו אלא ד' אמות - זהו ביוצא ברשות, אבל לדבר מצוה - יש לו אלפים אמה לכל רוח, כמ"ש בסימן ת"ז. ואפילו הניחוהו מחוץ לעיר ורוצה ליכנס, דכיון דלדבר מצוה נפיק - יש לו אלפים אמה לכל רוח, והוא הדין אף לדבר הרשות כשיצא בהיתר, והיינו ביום א' וב' וג' - גם כן הדין כן, כמ"ש שם. וכן אם היה שוגג והיה סבור שמותר לצאת ביום ד' וה' - גם כן מותר, דאומר מותר - שוגג הוא. (מג"א סקי"ח).