{"id":498,"date":"2022-08-30T15:02:21","date_gmt":"2022-08-30T15:02:21","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/emacs-shell-scripts-how-to-put-initial-options-into-the-script-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:02:21","modified_gmt":"2022-08-30T15:02:21","slug":"emacs-shell-scripts-how-to-put-initial-options-into-the-script-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/emacs-shell-scripts-how-to-put-initial-options-into-the-script-collection-of-common-programming-errors\/","title":{"rendered":"Emacs shell scripts &#8211; how to put initial options into the script?-Collection of common programming errors"},"content":{"rendered":"<p>Inspired by Stack\u00a0Overflow question Idomatic batch processing of text in Emacs? I tried out an Emacs shell script with the following headline:<\/p>\n<pre><code>#!\/usr\/bin\/emacs --script \n<\/code><\/pre>\n<p>I put some Emacs\u00a0Lisp code in it, and saved it as textfile rcat.<\/p>\n<p>Since the &#8211;script option does not prevent the loading of the site-start file, I had a lot of<\/p>\n<pre><code>Loading \/etc\/emacs\/site-start.d\/20apel.el (source)...\nLoading \/etc\/emacs23\/site-start.d\/35elib-startup.el (source)...\nLoading \/etc\/emacs23\/site-start.d\/50auctex.el (source)...\n<\/code><\/pre>\n<p>messages in the Bash shell (stdout). I can prevent that by calling<\/p>\n<pre><code>rcat --no-site-file\n<\/code><\/pre>\n<p>or<\/p>\n<pre><code>rcat -Q\n<\/code><\/pre>\n<p>but not by changing the headline in the script:<\/p>\n<pre><code> #!\/usr\/bin\/emacs --script --no-site-file\n<\/code><\/pre>\n<p>Is there a way to pass additional options to Emacs inside such a script file instead of doing it later on the commandline?<\/p>\n<ol>\n<li>\n<p>You could always change <code>#!\/usr\/bin\/emacs<\/code> to something like <code>#!\/home\/thorsten\/bin\/emacs-no-site-file<\/code>, and set that up as:<\/p>\n<pre><code>#!\/bin\/sh\nexec \/usr\/bin\/emacs --no-site-file \"$@\"\n<\/code><\/pre>\n<p>If this doesn&#8217;t work for you, or if you wish your script to be portable, then see Gilles&#8217; answer below for a more robust (and slightly funky \ud83d\ude42 approach.<\/p>\n<\/li>\n<li>\n<p>If you run emacs in script mode, I would recommend to reset &#8220;argv&#8221; variable to nil in the end of your script, otherwise emacs will try interpret &#8220;argv&#8221; after script is finished.<\/p>\n<p>Let&#8217;s assume you have a file named &#8220;test-emacs-script.el&#8221; with the following content:<\/p>\n<pre><code>#!\/usr\/bin\/emacs --script\n(print argv)\n(setq argv nil)\n<\/code><\/pre>\n<p>Try running this script as &#8220;.\/test-emacs-script.el -a&#8221;. If you run this script without resettings &#8220;argv&#8221; (last line in the script) then the output will be:<\/p>\n<pre><code>(\"-a\")\nUnknown option `-a'\n<\/code><\/pre>\n<p>Resetting &#8220;argv&#8221; gets rid of &#8220;unknown option&#8221; error message<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:47:45. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Inspired by Stack\u00a0Overflow question Idomatic batch processing of text in Emacs? I tried out an Emacs shell script with the following headline: #!\/usr\/bin\/emacs &#8211;script I put some Emacs\u00a0Lisp code in it, and saved it as textfile rcat. Since the &#8211;script option does not prevent the loading of the site-start file, I had a lot of [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-498","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/498","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=498"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/498\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=498"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=498"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=498"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}