top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to access command line arguments in shell script?

+1 vote
366 views
How to access command line arguments in shell script?
posted Oct 14, 2013 by Salil Agrawal

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

+5 votes

The arguments are stored in variables with a number in the order of the argument starting at 1

First Argument: $1
Second Argument: $2
Third Argument: $3
Example

command: ./script.bash alpha beta gamma

Variables: $1=='alpha'; $2=='beta'; $3=='gamma'

The variable $0 is the script's name. The total number of arguments is stored in $#. The variables $@ and $* return all the arguments.

answer Oct 14, 2013 by Vikas Upadhyay
Thanks Vikas, it was helpful
Similar Questions
+1 vote

I have some unique requirement, where I need to do something like this -
1. My script takes the argument as file name which is absolute filename.
2. I need to separate the filename and directory from this absolute file name for the further processing.

Any suggestions -

0 votes
+2 votes

I am using below script to validate IPV6 address but this script also pass invalid IP such as empty string, name etc.

if [[ "$IPv6ADDR"=~"^:?([a-fA-F0-9]{1,4}(:|.)?){0,8}(:|::)?([a-fA-F0-9]{1,4}(:|.)?){0,8}$" ]]; then
  echo "valid ipv6 addr"
  break;
else
  echo "Invalid IP Address"
  break;
fi

Can someone identify what's wrong in the regex, Please?

...