eolas/Programming_Languages/Shell/Passing_arguments_to_scripts.md

54 lines
2.3 KiB
Markdown
Raw Normal View History

2022-04-23 13:26:53 +01:00
---
2022-09-06 15:44:40 +01:00
categories:
- Programming Languages
2022-04-23 13:26:53 +01:00
tags:
- shell
---
2022-09-06 15:44:40 +01:00
2022-06-11 19:30:03 +01:00
# Passing arguments to scripts
2022-09-06 15:44:40 +01:00
2022-04-23 13:26:53 +01:00
## Relation between commands and programs
Whenever we issue a command in bash we are really running an executable program that is associated with the command. This is why when we create our own bash scripts we must run `chmod` to make them executables. When we issue a command like `./file.sh` we are running an executable program.
2022-12-07 07:00:04 +00:00
How come, however, that when we use a program like `cd` or `npm` we dont have to type `./cd.sh` or `./npm.sh` ? Remember from our discussion of the `PATH` environment variable that whenever we use inbuilt commands like `ls` and `cd` we are automatically sourcing them from the binary directory because we have these directories in our `PATH` . Hence the shell knows in advance what these commands mean. In the case of custom scripts, these arent typically added to the `PATH` so we have to source them in order to run them.
2022-04-23 13:26:53 +01:00
## Passing arguments
If you think about it, a script is really just a function that runs when you source it. As such there needs to be a way for you to pass data to the function so that it can actually act like a function and take arguments. When we use for example `cd ./Desktop` we are passing a directory name as an argument to the `cd` program. We can do the same thing with our custom bash scripts.
To pass an argument we simply add the values after the script in the command. For example:
2022-09-06 15:44:40 +01:00
```bash
2022-04-23 13:26:53 +01:00
./arguments.sh Thomas 33
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
The script is as follows:
2022-09-06 15:44:40 +01:00
```bash
2022-04-23 13:26:53 +01:00
#!/bin/bash
echo "File is called $0"
echo "The arguments provided are $@"
echo "The first argument is $1"
echo "The second argument is $2"
echo "Your name is $1 and you are $2 years old"
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
This outputs:
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
File is called ./arguments.sh
The arguments provided are Thomas 33
The first argument is Thomas
The second argument is 33
Your name is Thomas and you are 33 years old
2022-09-06 15:44:40 +01:00
```
2022-04-23 13:26:53 +01:00
Some points to note on syntax. The `$` is used to individuate the script itself and its arguments.
2022-09-06 15:44:40 +01:00
- Each argument passed is accessible from an index starting at `1` (`$1`)
- The script itself occupies the `0` position, hence we are able to log the name of the script at line 1 `$0` )
- To log the arguments as a group (for instance to later loop through them) we use `$@` .
- To get the number of arguments use `$#`