Last Updated: December 29, 2022
·
200.4K
· mehikmat

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

2 Responses
Add your response

$? 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 ·
  NODES
Community 1
iOS 2
Javascript 3
Note 1
os 8
web 1