08 Input Output - kumar159man/MyShellLearning GitHub Wiki

Input Output commands

As we are aware echo is the output command

Read input from a command line

myubuntu@myubuntu-VirtualBox:~$ read myname
Manish
myubuntu@myubuntu-VirtualBox:~$ echo $myname 
Manish

read command is used to read variables from command line

Read input from a command line with prompt

myubuntu@myubuntu-VirtualBox:~$ read -p "What is your pet's name " petsName
What is your pet's name Chelsea
myubuntu@myubuntu-VirtualBox:~$ echo $petsName
Chelsea

Default variable for read

If we don't give any variable name after read then the value get stored in default variable REPLY

myubuntu@myubuntu-VirtualBox:~$ read -p "Which shell you are using "
Which shell you are using bash
myubuntu@myubuntu-VirtualBox:~$ echo $REPLY
bash

Command Line /Positional arguments

Let's write a shell script to understand this

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat cmdArg.sh 
#!/usr/bin/bash
echo $0
echo $1
echo $2
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ./cmdArg.sh first second
./cmdArg.sh
first
second

$0 ->script name $1 ->first argument

How to pass space in arguments

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ./cmdArg.sh "first arguments" "second arguments"
./cmdArg.sh
first arguments
second arguments

How to get number of command line arguments

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat cmdArg.sh 
#!/usr/bin/bash
echo $0
echo $1
echo $2
echo "Number of arguments $#"
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ./cmdArg.sh "first arguments" "second arguments"
./cmdArg.sh
first arguments
second arguments
Number of arguments 2
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ 

How to print all the command line arguments

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat cmdArg.sh 
#!/usr/bin/bash
echo $0
echo $1
echo $2
echo "Number of arguments $#"
echo "Argument list $@"
echo "Argument list $*"
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ./cmdArg.sh "first arguments" "second arguments"clear
./cmdArg.sh
first arguments
second argumentsclear
Number of arguments 2
Argument list first arguments second argumentsclear
Argument list first arguments second argumentsclear

use {} for positional arguments >=10

myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat cmdArg.sh 
#!/usr/bin/bash
echo $0
echo $1
echo $2
echo $10
echo "Number of arguments $#"
echo "Argument list $@"
echo "Argument list $*"
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ./cmdArg.sh first second 3 4 5 6 7 8 9 10
./cmdArg.sh
first
second
first0
Number of arguments 10
Argument list first second 3 4 5 6 7 8 9 10
Argument list first second 3 4 5 6 7 8 9 10
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ cat cmdArg.sh 
#!/usr/bin/bash
echo $0
echo $1
echo $2
echo ${10}
echo "Number of arguments $#"
echo "Argument list $@"
echo "Argument list $*"
myubuntu@myubuntu-VirtualBox:~/Desktop/shellScripts$ ./cmdArg.sh first second 3 4 5 6 7 8 9 10
./cmdArg.sh
first
second
10
Number of arguments 10
Argument list first second 3 4 5 6 7 8 9 10
Argument list first second 3 4 5 6 7 8 9 10
⚠️ **GitHub.com Fallback** ⚠️