4 ms·
> All scripts should be run with "-eu". More specifically, every shell script should have "set -eu" at its second line, directly after the sh'bang line: #
by vog 10y ago
> All scripts should be run with "-eu".
More specifically, every shell script should have "set -eu" at its second line, directly after the sh'bang line:
#!...
set -eu
For debugging or logging purposes, sometimes adding -v and/or -x comes handy:
#!...
set -euvx
- shawabawa3 10y agoYou can also do #!/bin/sh -eu Although i'm not sure if there are any advantages/disadvantages
- wahern 10y agoDisadvantage is that nobody can use an alternative shell to run your script as intended. (Alternative here could mean a less buggy version of whatever is the system shell.)
- zwp 10y ago> should have "set -eu" at its second line Do you have a strong reason to prefer "set" over shebang flags? I have a slight preference for shebang flags so I can deliberately override them from the command line (but it's not a hill I'd die on): $ cat foo #!/bin/bash -eu cd /nowhere echo 'still here' $ ./foo ./foo: line 2: cd: nowhere: No such file or directory $ bash -c ./foo # thinking about system(3) ./foo: line 2: cd: nowhere: No such file or directory $ bash +e ./foo # override ./foo: line 2: cd: nowhere: No such file or directory still here $ EDIT: Google's shell style guide has "Executables must start with #!/bin/bash and a minimum number of flags. Use set to set shell options so that calling your script as bash <script_name> does not break its functionality." https://google.github.io/styleguide/shell.xml?showone=Which_Shell_to_Use#Which_Shell_to_Use https://google.github.io/styleguide/shell.xml?showone=Which_...
- vog 10y agoBash is not always /bin/bash. On some systems (BSDs) it is /usr/bin/bash. So the portable sh'bang line for bash is: #!/usr/bin/env bash And there you can't add "-eu" anymore, because only one argument is possible in sh'bang lines. (/usr/bin/env would try to find an executable named "bash -eu") So #!/usr/bin/env bash set -eu is the more portable approach. However, for "/bin/sh" this should work portably: #!/bin/sh -eu
- saghm 10y ago> Bash is not always /bin/bash. On some systems (BSDs) it is /usr/bin/bash. Nitpick: on every BSD system I've used, it's /usr/local/bin/bash. But I definitely agree that using /usr/bin/env bash is the preferred solution for when you want to use bash (although I don't think it's even installed by default on most BSD's); as an Arch user (where /usr/bin/python is symlinked to python3 rather than python2), I appreciate those who go out of their way to use proper, portable shebangs.
- djsumdog 10y agoIs there a way to do this in Fish shell? So far the only thing I could find were bugs closed as duplicates that led to this open bug: https://github.com/fish-shell/fish-shell/issues/510 https://github.com/fish-shell/fish-shell/issues/510