Bash script arguments and functions: $1, $@, shift and return codes
A function receives its own positional parameters, which are restored when it returns; "$@" preserves argument boundaries.
Each command below is the exact command that was run and each output block is copied from the recorded log. Test ids, log hashes and exit statuses are in the evidence table.
TE-58-A: a function gets its own positional parameters, which are restored after it returns; quoted "$@" preserves arguments
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.sh <<'EOS'
f() { echo in=$#; printf '<%s>\n' "$@"; shift; echo after_shift=$#; printf '<%s>\n' "$@"; }
set -- one "two three"
f a b
echo after=$#
printf '[%s]\n' "$1"
g() { return 7; }
g
echo ret=$?
EOSCommand:
bash s.shRecorded output:
in=2
<a>
<b>
after_shift=1
<b>
after=2
[one]
ret=7Exit status: 0. This is the status recorded in the log for TE-58-A.
TE-58-B: a variable declared with declare inside a function does not change the variable of the same name outside
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.sh <<'EOS'
f() { declare x=2; echo in=$x; }
x=1
f
echo out=$x
EOSCommand:
bash s.shRecorded output:
in=2
out=1Exit status: 0. This status and the output match what was written before the run.
TE-58-C: expected non-zero exit: exit inside a function ends the whole script with that status
Setup for this run, inside a throwaway directory (mktemp -d):
cat > s.sh <<'EOS'
f() { exit 4; }
f
echo not_reached
EOSCommand:
bash s.shRecorded output: none, standard output was empty.
Exit status: 4. This status and the output match what was written before the run.
TE-58-B: declare inside the function made its own x with the value 2 and printed in=2, and the x outside the function is still 1, so out=1. TE-58-C: exit 4 inside the function ended the script with exit status 4, so not_reached was not printed and standard error was empty.
Limits
Unquoted $@ splits; return sets a status, exit ends the shell.
Source
Source: https://www.gnu.org/software/bash/manual/bash.html#Shell-Functions
Evidence
TE-58-A: a function gets its own positional parameters, which are restored after it returns; quoted "$@" preserves arguments
- logs: one recorded log for each row above
- Recorded on: Linux x86_64, kernel 7.3.4; bash 5.1.16; git 2.34.1; python 3.10.12; findutils 4.8.0; grep 3.7; sed 4.8; gawk 5.1.0; tar 1.34; curl 7.81.0. Locale C.UTF-8, TZ=UTC.
Evidence table
| Test | What it checks | Exit | Verdict | Log sha256 (first 12) | |---|---|---|---|---| | TE-58-A | a function gets its own positional parameters, which are restored after it returns; quoted "$@" preserves arguments | 0 | PASS | 49445e9cf59b | | TE-58-B | a variable declared with declare inside a function does not change the variable of the same name outside | 0 | PASS | a75dd2d16c5e | | TE-58-C | expected non-zero exit: exit inside a function ends the whole script with that status | 4 | PASS | 086d247ecf77 |
Every row has a recorded log holding the setup commands, the command, stdout, stderr, exit status and the final directory state. The first 12 characters of the log's sha256 are in the table.
All full guides: All guides