From the course: Linux System Engineer: Bash Shell Scripting for Automation

Positional arguments

From the course: Linux System Engineer: Bash Shell Scripting for Automation

Start my 1-month free trial

Positional arguments

- Shell scripts for automation aren't very useful unless you can get data in to them. Also, for creating tools to help make our job easier, we probably want them to look and act like real commands. Positional arguments are the first up and doing that. Most normal commands have positional arguments. For example, let's take a look at the ls command. Make sure you're in your tilda slash bin director. You now type in ls * and hit enter. Ls lists all files in the current directory because the shell is passing this list to ls by expanding the asterisk. Ls thinks that it was given a list of arguments even though we didn't type them in. Now, let's be more specific. Type in ls debugger.sh script.sh and hit enter. Now we've passed two arguments to ls directly. Instead of allowing the shell to expand the wild card. These two arguments get passed to ls and then acts on them accordingly. Let's simulate this in a script. Let's create a new script named posargs.sh Type in vi posargs.sh and hit enter. Going to insert mode and add #!/bin/bash and then a new line and then type in echo '$0 is ' "$0". Make sure your quotes are identical to mine otherwise it won't work. Press escape, put your cursor on the line and press yy to copy the line in to the buffer. Now press p four times. We'll change the first copy line to reference dollar sign one. Go in to insert mode and change it to dollar sign one. We'll change the second line to references dollar sign two. We'll change the third line to reference dollar sign at. And we'll change the last line to reference dollar sign asterisk. So it should look like this. Always quote your variables. We used a single quote so the first variable on the line will not be processed and the literal names will be printed on the screen. The second variable on the line is double quoted which reserves spaces but allows variables to be expanded. Now save it by pressing escape, colon, w, exclamation mark and hit enter. To make development faster I'm going to open a second terminal and run it from there. Make sure you're in tilda slash bin and then type in chmod u+x posargs.sh and hit enter. Now run it by typing in posargs.sh and hit enter. We get some confusing output because we didn't pass in any positional arguments. Let's do it again with arguments. Bring your line back and type in dog cat horse. So you're lines should say, posargs.sh dog cat horse and hit enter. Now, let's look at the output. $0 is /home/user/bin/posargs.sh. Dollar sign zero is the path to the script. The next two lines are dollar sign one is dog and dollar sign two is cat. Dollar sign one is the first argument, in this case it's dog. Dollar sign two is the second argument, in this case it's cat. The next two lines, dollar sign at is dog cat horse and dollar sign asterisk is dog cat horse look identical. Dollar sign at is all arguments. Dollar sign asterisk is also all arguments. The difference between the two last lines when their not quoted is the same. However, you should never use either unquoted, as they won't preserve spaces. In this script we've quoted them both which changes their functionality. In this case dollar sign asterisk contains one thing. All arguments has one entity. If we were to loop through all items in dollar sign asterisk. It would iterated once as it appears at one item. Where as, dollar sign at contains word split items. Each one essentially quoted. If we were to loop through dollar sign at it would iterate three times as each item is a separate thing.

Contents