BASH allows you to access arguments passed to your script using positional parameters. These work just like variables except that their names are numbers that correspond to the order an argument was passed to the script.
For example, $0 is the name of the script, $1 is the first argument passed to the script, $2 is the second argument, and so on.
Positional parameters are used in place of what other languages like C refer to as the argument vector (passed to the main function as char argv ).
$0 $1 $2 $3
#!/bin/bash #Print all arguments to the script echo $@ #Print the name of the script echo $0 #Print the first argument to the script echo $1 #Print the number of arguments passed to the script echo $#
The read built-in
read [options] variable
#!/bin/bash # This script demonstrates the use of the BASH read built-in read username echo "$username" # Only read n characters read -n 1 value echo "$value" # Read with a prompt read -p "Please enter your name: " name echo "$name"
- Write a script that will prompt the user for a filename and print it to stdout (standard out).
- Write a script that reads only the first two characters of user input.
- Write a script that will prompt the user and accept input in the same command.
- Submit the Argumentative tutorial challenge.