The shell command and any arguments to that command appear as numbered shell variables: $0 has the string value of the command itself, something like script, ./script, /home/user/bin/script or whatever. Any arguments appear as "$1", "$2", "$3" and so on. The count of arguments is in the shell variable "$#". Common ways of dealing with this involve shell commands getopts and shift. getopts is a ...
What does $# mean in shell? I have code such as if [ $# -eq 0 ] then I want to understand what $# means, but Google search is very bad for searching these kinds of things.
This latter usage is faster, does not contaminate the shell's variable namespace with what amounts to temp variables, can often be a lot more readable for humans and encourages the use of "positive logic", the practice of writing conditionals without negations, which has cognitive simplicity in most situations.
It is rarely a good idea to have sudo inside scripts. Instead, remove the sudo from the script and run the script itself with sudo: sudo myscript.sh That way, all commands within the script will be run with root privileges and you only need to give the password once when launching the script. If you need a particular command within the script to be run without sudo privileges, you can run it ...
The relevant man page to check for this is that of the shell itself, bash, because -f is functionality that the shell provides, it's a bash built-in. On my system (CentOS 7), the fine man page covers it. The grep may not give the same results on other distributions. Nevertheless, if you run man bash and then search for '-f' it should give the results you require. $ man bash | grep -A1 '\-f ...
I'm having trouble with escaping characters in bash. I'd like to escape single and double quotes while running a command under a different user. For the purposes of this question let's say I want t...
The variable will be set for the rest of the shell session or until unset. To set an environment variable everytime, use the export command in the .bashrc file (or the appropriate initialization file for your shell). To set an environment variable from a script, use the export command in the script, and then source the script.
A great IBM article here goes into more detail on the evolution of shells in Linux and describes well the shell architecture and how shells differ. Shell Scripting Sh was the original Unix shell developed by Stephen Bourne; however Debian based systems and Ubuntu considers dash as their sh shell (sh is actually symlinked to dash).