Let's say you have a bash script with name script.sh with a simple hello message.sample.sh
#!/bin/bash echo "hello!"
Now if you want to have this script accept command line string arguments, you can simply add it when you run your script.
# ./script.sh Sam
You will see this prints out only "hello!" because we have not made use of the argument passed in the script, so lets modify it.
#!/bin/bash echo "hello $1!"
Now lets run the script again by passing our 1st argument.
# ./script.sh Sam hello Sam!
There we have it!
If you add more arguments to the bash script when executing you can access them using $2, $3, $4... and so on!
If you want to fetch the name of the script you can make use of $0Example:
#!/bin/bash echo "hello $1! you just executed the script: $0!"
hello Sam! you just executed the script: script.sh
Facing issues? Have Questions? Post them here! I am happy to answer!
- Bash Hello World! Script Tutorial
- How to run bash command in background
- How to create new user account in Windows bash
- How to Compare Strings in Bash
- Bash: Allow Command to Fail without exiting Script
- Fix bash: script.sh: Permission denied Error
- Bash getopts Command Example
- How to fix bash ping command not found error
- How to add NewLine Character in Bash Script String
- How to Echo Bash Command to a File
- Bash Command to Find String in a File
- How to open new Terminal using Bash Command
- Bash How to Save Output of a Command to a Variable
- Download a SSL Certificate from a URL in Terminal
- Bash Command to Check IP Address
- Know Bash shell version command
- Bash Command To Check If File Exists
- How to run a Command in Bash Script
- How to Compare two Files in Bash Shell
- How to check if a variable is set in Bash Script or Not
- Convert String from uppercase to lowercase in Bash
- How to see Created Accessed Modified and Changed dates of a file using bash terminal command
- Bash Command To Get Current Time
- Bash command to List Files
- Fix: bash: syntax error near unexpected token
- How to enable Do Not Disturb mode for Notification Center in Mac OS X 10.10 Yosemite - Mac-OS-X
- Shortcut: Move Cursor at the start or end of Line or file in Visual Studio Code (VS Code) - Shortcuts
- [Gmail] Calling in Hangouts will be turned off soon. - News
- How to reset AirPods or AirPods Pro using iPhone/iPad or iPod - iOS
- Add Line Break in Microsoft Excel Cell on Mac (macOS) - MacOS
- Sort a List using Java 8 Stream Examples - Java
- Right Align Text in Bootstrap framework - Bootstrap
- Turn off Startup Sound on Mac running on macOS Ventura - MacOS