In a bash script, how to reference arguments?
Jump to:
Project: | Linux software |
Component: | Documentation |
Category: | support request |
Priority: | normal |
Assigned: | Unassigned |
Status: | closed |
Related pages: | #303: Bash |
Description
In a bash script, how to reference arguments?
If I do
$ myscript.sh file.txt
How can I reference to the argument 'file.txt'?
Comments
#1
Check the following section of the official documentation:
3.4.1 Positional Parameters
http://www.gnu.org/software/bash/manual/bash.html#Positional-Parameters
"A positional parameter is a parameter denoted by one or more digits, other than the single digit 0. Positional parameters are assigned from the shell’s arguments when it is invoked, and may be reassigned using the set builtin command. Positional parameter N may be referenced as ${N}, or as $N when N consists of a single digit."
Thus the simple script:
#!/bin/bash
echo ${1}
would work this way:
$ myscript.sh test.txt
test.txt
#2
Use quotation marks in case there are spaces in the file name:
#!/bin/bash
cat "${1}"
#3
Automatically closed -- issue fixed for 2 weeks with no activity.