I ran this command in shell script-
echo grep datadir /etc/my.cnf
this is the output i got-
datadir=/var/lib/mysql
What should I do to get the output as-
/var/lib/mysql
How about:
[cnicutar@fresh ~]$ echo $str
datadir=/var/lib/mysql
[cnicutar@fresh ~]$ echo ${str#*=}
/var/lib/mysql
Read more about these operations at the ABS.
Pipe the output of the grep in a cut:
$ echo 'datadir=/var/lib/mysql' | cut -d '=' -f 2
/var/lib/mysql
You might run with problems if there are spaces in your file though (note the indent on the output):
$ echo 'datadir = /var/lib/mysql' | cut -d '=' -f 2
/var/lib/mysql
You could use awk to circumvent this:
$ echo 'datadir = /var/lib/mysql' | awk -F '[[:space:]]*=[[:space:]]*' '{print $2}'
/var/lib/mysql
Pipe it through sed 's/^datadir=//' which means "replace the leading datadir= with nothing".
sed -n 's/^datadir=//p' /etc/my.cnf without any grep or echo.
echois wrong, or you have omitted a (useless) pair of backticks.