Files
linux.softwareshinobi.com/landing/docs/.Bash-Scripting/007-bash-arguments.md
Software Shinobi aa880afc23
All checks were successful
learn org at code.softwareshinobi.com/linux.softwareshinobi.com/pipeline/head This commit looks good
automated push from the terminal
2025-06-18 09:50:07 -04:00

82 lines
1.6 KiB
Markdown

# Bash Arguments
You can pass arguments to your shell script when you execute it. To pass an argument, you just need to write it right after the name of your script. For example:
```bash
./devdojo.com your_argument
```
In the script, we can then use `$1` in order to reference the first argument that we specified.
If we pass a second argument, it would be available as `$2` and so on.
Let's create a short script called `arguments.sh` as an example:
```bash
#!/bin/bash
echo "Argument one is $1"
echo "Argument two is $2"
echo "Argument three is $3"
```
Save the file and make it executable:
```bash
chmod +x arguments.sh
```
Then run the file and pass **3** arguments:
```bash
./arguments.sh dog cat bird
```
The output that you would get would be:
```bash
Argument one is dog
Argument two is cat
Argument three is bird
```
To reference all arguments, you can use `$@`:
```bash
#!/bin/bash
echo "All arguments: $@"
```
If you run the script again:
```bash
./arguments.sh dog cat bird
```
You will get the following output:
```
All arguments: dog cat bird
```
Another thing that you need to keep in mind is that `$0` is used to reference the script itself.
This is an excellent way to create self destruct the file if you need to or just get the name of the script.
For example, let's create a script that prints out the name of the file and deletes the file after that:
```bash
#!/bin/bash
echo "The name of the file is: $0 and it is going to be self-deleted."
rm -f $0
```
You need to be careful with the self deletion and ensure that you have your script backed up before you self-delete it.