$#, $@ & $?: Bash Built-in variables
I found the $#, $@ & $? bash built-in variables very useful since I knew linux and today I would like to share their usage. I hope that you don't mind if you already know.
Example:
file:test.sh
#! /bin/sh
echo '$#' $#
echo '$@' $@
echo '$?' $?
*If you run the above script as*
> ./test.sh 1 2 3
You get output:
$# 3
$@ 1 2 3
$? 0
*You passed 3 parameters to your script.*
$# = number of arguments. Answer is 3
$@ = what parameters were passed. Answer is 1 2 3
$? = was last command successful. Answer is 0 which means 'yes'
Written by Hikmat Dhamee
Related protips
2 Responses
$? is the exit status of the last function. 0 is a common exit code to denote the program exited without any issues.
over 1 year ago
·
Thank you for sharing your observations.
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Shell
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#