Search
 
SCRIPT & CODE EXAMPLE
 

SHELL

shell script get arguments

$ myscript.sh first_arg second_arg

# myscript.sh
#!/bin/bash
echo $1 # output: first_arg
echo $2 # output: second_arg
Comment

linux shell arguments

# bash arguments cheatsheet
$1, $2, ... -> are the positional parameters.
"$@"        -> is an array-like construct of all positional parameters, {$1, $2, $3 ...}.
"$*"        -> is the IFS expansion of all positional parameters, $1 $2 $3 ....
$#          -> is the number of positional parameters.
$-          -> current options set for the shell.
$$          -> pid of the current shell (not subshell).
$_          -> most recent parameter (or the abs path of the command to start the current shell immediately after startup).
$IFS        -> is the (input) field separator.
$?          -> is the most recent foreground pipeline exit status.
$!          -> is the PID of the most recent background command.
$0          -> is the name of the shell or shell script.
Comment

command line argument bash

bash yourscript.sh file1 file2

yourscript.sh
inside script 

#!/bin/bash
a=$1     # = to file1
b=$2     # = file2
echo $1
echo $2
Comment

shell parse command line arguments

cat >/tmp/demo-space-separated.sh <<'EOF'
#!/bin/bash

POSITIONAL_ARGS=()

while [[ $# -gt 0 ]]; do
  case $1 in
    -e|--extension)
      EXTENSION="$2"
      shift # past argument
      shift # past value
      ;;
    -s|--searchpath)
      SEARCHPATH="$2"
      shift # past argument
      shift # past value
      ;;
    --default)
      DEFAULT=YES
      shift # past argument
      ;;
    -*|--*)
      echo "Unknown option $1"
      exit 1
      ;;
    *)
      POSITIONAL_ARGS+=("$1") # save positional arg
      shift # past argument
      ;;
  esac
done

set -- "${POSITIONAL_ARGS[@]}" # restore positional parameters

echo "FILE EXTENSION  = ${EXTENSION}"
echo "SEARCH PATH     = ${SEARCHPATH}"
echo "DEFAULT         = ${DEFAULT}"
echo "Number files in SEARCH PATH with EXTENSION:" $(ls -1 "${SEARCHPATH}"/*."${EXTENSION}" | wc -l)

if [[ -n $1 ]]; then
    echo "Last line of file specified as non-opt/last argument:"
    tail -1 "$1"
fi
EOF

chmod +x /tmp/demo-space-separated.sh

/tmp/demo-space-separated.sh -e conf -s /etc /etc/hosts
Comment

PREVIOUS NEXT
Code Example
Shell :: find pip (s) path 
Shell :: install wordpress on ubuntu 20.04 
Shell :: uploading a folder to github 
Shell :: install all dependencies npm 
Shell :: utserver: error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory 
Shell :: remove folder from repo but keep locally 
Shell :: ping: socket: Operation not permitted 
Shell :: github activity graph 
Shell :: switch from npm to yarn 
Shell :: push code to github 
Shell :: install groovy on ubuntu 
Shell :: install packages from jupyter notebook 
Shell :: ubuntu service start example 
Shell :: # Check failed: allocator-SetPermissions(reinterpret_cast<void*(region.begin()), region.size(), PageAllocator::kNoAccess). 
Shell :: edit file terminal 
Shell :: install krew 
Shell :: installing a downloaded package in ubuntu 
Shell :: ubuntu dns config 
Shell :: git clone command 
Shell :: scheduler ubuntu cmd 
Shell :: apt lock 
Shell :: bash rm all except 
Shell :: remove untracked files 
Shell :: check apache version ubuntu 
Shell :: square root in bash script 
Shell :: scp HostKeyVerification=false 
Shell :: pem to crt 
Shell :: docker clear disk 
Shell :: git remove file from tracking without deleting 
Shell :: installing docker curl wget 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =