Why quotes in shell script didn't return, help me cd to directory

I have a shell script with the following code:

dir=sample
`mkdir $dir`
`cp /home/bhavya/workspace/UnetStack/logs/log-0.txt $dir/log.txt`
`cd $dir`

On the last line with the command cdin quotation marks, I could not go to cdthe corresponding directory. But as soon as I removed the backquotes, I was able to cd. What I was wondering why doesn’t work cdwith reverse quotation?

+5
source share
2 answers

When you started:

`mkdir $dir`

the shell first executed a command mkdir $dirin a subshell, capturing its (standard) output, and then executed the captured line as a command. Fortunately, the solution was empty, so the second step did nothing.

When you started:

`cp /home/bhavya/workspace/UnetStack/logs/log-0.txt $dir/log.txt`

, . , , .

:

`cd $dir`

, cd , , ( Unix, DOS .bat). , cd , , .

, , .

:

dir=sample
mkdir $dir
cp /home/bhavya/workspace/UnetStack/logs/log-0.txt $dir/log.txt
cd $dir
...other activity in the new directory...

, script, script . , - . ( bash, source, ).

(, , $(...)) . :

gcc_lib_dir=$(dirname $(dirname $(which gcc)))/lib

which gcc; /usr/gcc/v4.7.1/bin/gcc; dirname /usr/gcc/v4.7.1/bin; dirname /usr/gcc/v4.7.1; /lib

gcc_lib_dir=/usr/gcc/v4.7.1/lib

, $(...) :

gcc_lib_dir=`dirname \`dirname \\\`which gcc\\\`\``/lib

, !

+13

Backquotes . , script.

+5

All Articles