1

I am not much familiar with shell script. I want to modify a script but it's giving me error "No such file or directory" after I change it.

But that command works over the command prompt.

Here is the line which causes problem. (Even not sure how below command spawns a process.)

_T_COMMAND_=1 "valgrind ---tool=memcheck --trace-children=yes command"

same thing works if I run command as

$valgrind ---tool=memcheck --trace-children=yes command

Any idea?

3
  • To have a command executed, you need to do var=$(command). Hence, you need something like _T_COMMAND_=$(valgrind ---tool=memcheck --trace-children=yes command) Commented Sep 27, 2013 at 10:03
  • but, it is working with _T_COMMAND_=1 "command". which I had modified to _T_COMMAND_=1 "valgrind ---tool=memcheck --trace-children=yes command" Commented Sep 27, 2013 at 10:06
  • 1
    Gave up script change... Fix the issue with cpp code change with adding valgrind to specific child process !!! Commented Sep 29, 2013 at 6:26

1 Answer 1

2

Don't put the command in double quotes.

_T_COMMAND_=1 valgrind ---tool=memcheck --trace-children=yes command

The general syntax is simply

[var=value ...] cmd [args]

which will set the environment variable var to value for the duration of cmd. You can set several variables in this way.

Alternatively, set the variable and export it; then it will remain set for the remainder of the current shell's lifetime, and be exposed to subprocesses (that's what the export does).

_T_COMMAND_=1
export _T_COMMAND_
valgrind ---tool=memcheck --trace-children=yes command

Similarly, valgrind processes its options, then runs the specified command (with any options) as a subprocess.

A single command in double quotes is harmless, because the shell will strip the quotes before the kernel sees the argument. A string with spaces in double quotes will be preserved as a single argument, while without quotes, it becomes multiple arguments. Behold:

bash$ perl -le 'print "<<$_>>" for @ARGV' "foo bar" baz quux
<<foo bar>>
<<baz>>
<<quux>>

or just as well, add harmless but no doubt rather confusing double quotes around everything which isn't already quoted:

bash$ "perl" "-le" 'print "<<$_>>" for @ARGV' "yowza"
<<yowza>>

The shell parses this into

<<perl>>
<<-le>>
<<print "<<$_>>" for @ARGV>>
<<yowza>>

and removes the (outer) quotes in the process.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.