For example:
I want user to input A=a
and I have the command which I guess is totally wrong.
read -p "Enter something:" frsstring=secstring
echo $frsstring
echo $secstring
````````````````````````````````````````````````````````````
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityIn bash
you can use an array variable and change the IFS (internal field separator) special variable to =
which normally contains space character, tab and newline to split words.
IFS='=' read -a arr -p "Enter something: "
echo "${arr[0]}"
echo "${arr[1]}"
Or you can use shell parameter expansion to remove the longest suffix and prefix pattern from the string:
read -p "Enter something: " str
echo "${str%%=*}" # remove longest suffix pattern `=*`
echo "${str##*=}" # remove longest prefix pattern `*=`
I don't know how could it be done with one command, but you could read the entire line, and then split it in the desired variables:
#!/bin/bash
read -p "Enter something:" line
frsstring=`echo "$line" | cut -f1 -d'='`
secstring=`echo "$line" | cut -f2 -d'='`
echo $frsstring
echo $secstring
I hope it could help
=
between the two variable names. If you want the user to enter two values separated by and=
symbol, setIFS='='
before the read. e.g.IFS== read -p "Enter something:" frsstring secstring