If I ask bash to echo the -1th argument it prints hb1:
echo $-1
hb1
Why? What is it accessing?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIf I ask bash to echo the -1th argument it prints hb1:
echo $-1
hb1
Why? What is it accessing?
You are not asking it to print the 1st argument, that would be: $1
.
What you are asking for is a special parameter:
-
(
$-
, a hyphen.) Expands to the current option flags as specified upon invocation, by the set builtin command, or those set by the shell itself (such as the -i option).
So your options are: hb
Then you see the 1
you've added is printed afterwards (hence hb1
).
If you are looking to get the last argument (Not sure if that is what you meant by -1 argument), you can use Shell Parameter Expansion in the following form:
$ set -- one two three
$ echo "${@: -1}"
three
${!#}
is also the value of the last parameter, using indirect expansion.
Jun 22, 2019 at 19:30
${@:~0}
and eval echo \$$#
are also the last argument. :-)
IFS
contains any digits in the value $#
expands to, not so much. Always safer to quote; eval "echo \"\$$#\""
is more reliable. To see the failure for yourself: IFS=1234567890; set -- one two three; eval echo \$$#
only outputs $
.
Jun 23, 2019 at 20:10
IFS=1234567890; set -- {a..w}; eval echo \"\$\{"$#"\}\"
;-)
$-
gets the current shell options (which is apparently "hb"). What did you expect it to do?