I'm reading up on exec
and how to use the -a
flag. The SS64 docs seem accurate; they say the following:
exec:
Execute a command
Syntax:
exec [-cl] [-a name] [command [arguments]]
Options:
-c Causes command to be executed with an empty environment. -l Place a dash at the beginning of the zeroth arg passed to command. (This is what the login program does.) -a The shell passes name as the zeroth argument to command.
To test this out, I wrote two scripts, one named foo/baz
and one named foo/buzz
:
#!/usr/bin/env bash
# foo/baz
exec -a blah ./foo/bar 1 2
#!/usr/bin/env bash
# foo/buzz
exec ./foo/bar 1 2
Each of these scripts runs the same child script, foo/bar
, which does the following:
#!/usr/bin/env bash
echo "Hello world"
echo "0: $0"
echo "1: $1"
echo "2: $2"
My goal is to see what effect -a
has on the 0th and subsequent arguments. If -a
causes the 0th argument to change to the argument you pass to the -a
flag, then I would expect the 0th argument when I run foo/baz
to be blah
, since that's what I pass to -a
. However, when I run the scripts, the output is the same in both cases:
~/Workspace/OpenSource (master) $ ./foo/baz
Hello world
0: /Users/richiethomas/Workspace/OpenSource/foo/bar
1: 1
2: 2
~/Workspace/OpenSource (master) $ ./foo/buzz
Hello world
0: /Users/richiethomas/Workspace/OpenSource/foo/bar
1: 1
2: 2
Am I doing something wrong? Or is my expectation incorrect somehow?
Also, a related question- what is the use case of overriding the 0th argument, as opposed to just accessing any passed-in args via $1, $2, $3, etc.?
exec -a SomeName sh -c 'echo $0'
.argv[0]