1toN

Special Shell Variables

Introduction

user

Rafael Lopes (?)

Tech-lover, also loves photography and curiosity. AWS Cloud Ninja. What I enjoy? Learn from unknown internet blogs like this one.


Shell

Special Shell Variables

Posted by Rafael Lopes on .
Featured

Shell

Special Shell Variables

Posted by Rafael Lopes on .

Do you know that shell has some willy-wonka-like variables? Why not consider them while writing your own scripts?

  • $1 $2 $3 $4 $5 $6 $7 $8 $9 are positional parameters, the parameters received when the program is called. If you want to use more than 9 arguments, just use braces, like ${10}, ${11};
  • $0 returns the name of the command currently being executed (it´s like the zero positional parameter, doesn´t make sense?): in a shellscript, you can combine it with basename $0 to print the current script name without any dots or else;
  • $# is the number of positional arguments given to this invocation of the shell;
  • $? returns the exit status (or function returns) of the last command executed is given as a decimal; string. When a command completes successfully, generally it returns the exit status of 0 (zero) if everything was ok, otherwise it returns a non-zero exit status, I said generally;
  • $$ represents the process number of this shell – useful for including in filenames, to make them unique, or just anything you want to make unique;
  • $! tell you the process id of the last command run in the background;
  • $- the current options supplied to this invocation of the shell;
  • $* represents a string separated by spaces containing all the arguments passed to the shell via positional parameters, starting at $1;
  • $@ is the same as above, except when quoted.
user

Rafael Lopes (?)

Tech-lover, also loves photography and curiosity. AWS Cloud Ninja. What I enjoy? Learn from unknown internet blogs like this one.