# Bash script arguments and functions: $1, $@, shift and return codes

Published: 2026-11-30

Rights: original
Source: https://kairos-2z6s.onrender.com/res/bash-arguments-functions-full-en
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.

(Test: 3 tests were run, TE-58-A, TE-58-B and TE-58-C. The expected non-zero exit is TE-58-C (exit status 4). Every row has a recorded log, and the exit statuses and log hashes 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`):

```bash
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=$?
EOS
```

Command:

```bash
bash s.sh
```

Recorded output:

```
in=2
<a>
<b>
after_shift=1
<b>
after=2
[one]
ret=7
```

Exit 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`):

```bash
cat > s.sh <<'EOS'
f() { declare x=2; echo in=$x; }
x=1
f
echo out=$x
EOS
```

Command:

```bash
bash s.sh
```

Recorded output:

```
in=2
out=1
```

Exit 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`):

```bash
cat > s.sh <<'EOS'
f() { exit 4; }
f
echo not_reached
EOS
```

Command:

```bash
bash s.sh
```

Recorded 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.

## Related guides
- [Bash conditionals: [ ] and [[ ]] with an empty variable](https://kairos-2z6s.onrender.com/res/bash-conditionals-full-en)
- [Pipes and redirection: where command output goes](https://kairos-2z6s.onrender.com/res/pipes-redirection-full-en)
- [awk basics: split records into fields](https://kairos-2z6s.onrender.com/res/awk-basics-full-en)
- [Bash exit codes: 0 is success, and && and || use it](https://kairos-2z6s.onrender.com/res/bash-exit-codes-full-en)
- [diff: see what changed between two files](https://kairos-2z6s.onrender.com/res/diff-basics-full-en)
