85jnpq
Last Updated: September 14, 2017
·
14.19K
· mehikmat
587e337464192cb0425836412f98970a

$#, $@ & $?: 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'
Say Thanks
Respond

1 Response
Add your response

29298

$? is the exit status of the last function. 0 is a common exit code to denote the program exited without any issues.

5 months ago ·
Filed Under