What does the “$#” special parameter mean in bash?

2019-02-24 22:31发布

Ive come across the code

if [ $# -eq 1  ]; then
   echo "usage: Phar ~/flashmem ~/archive"
   exit
fi

Ive never come across [ $# -eq 1 ]; before and I cant seem to find a meaning

2条回答
Melony?
2楼-- · 2019-02-24 23:12

The "$#" return the number of parameters passed as argument

#!/bin/bash
echo $#

Now

./testess.sh test1 test2 test3

this return 3

./testess.sh test1 test2 test3 test4 test5

this return 5

So in your code if "$#" equal the number one (just one argument passed), execute the echo command

查看更多
做个烂人
3楼-- · 2019-02-24 23:22

It Expands to the number of positional parameters in decimal.

(copied from man bash, under Special Parameters).

查看更多
登录 后发表回答