Use a shell variable to execute a command

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

up vote
4
down vote

favorite

I have a unix command in a variable, it looks like this:

cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep $@"
`$cmd`
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

When I try to execute the command $cmd in a bash script, it won’t work. However, when I copy and paste the exact same command, it does work. Can you let me know what I am doing wrong?

I have tried putting quotes around the path, same error occurs

cmd="find "/path/to/webpage" -type f | grep -v .svn | xargs grep $@"
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

When I remove the -type f parameter, I get this error:

cmd="find /path/to/webpage | grep -v .svn | xargs grep $@"
find: invalid predicate `-v'

That makes me think that the pipe is not being recognized. What can I do to get this to work?

share|improve this question

  • 1

    Long command pipelines in shell variables should make anyone nervous. What are you trying to do? I recommend taking some time to read and digest BashFAQ 50 before taking this approach any further.
    – jw013
    Jul 26 ’12 at 20:19

  • are you trying to execute the contents of a variable or assign the output of a command to a variable? Your question says the former, but your response to Tim’s answer says the latter.
    – cas
    Jul 26 ’12 at 22:25

up vote
4
down vote

favorite

I have a unix command in a variable, it looks like this:

cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep $@"
`$cmd`
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

When I try to execute the command $cmd in a bash script, it won’t work. However, when I copy and paste the exact same command, it does work. Can you let me know what I am doing wrong?

I have tried putting quotes around the path, same error occurs

cmd="find "/path/to/webpage" -type f | grep -v .svn | xargs grep $@"
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

When I remove the -type f parameter, I get this error:

cmd="find /path/to/webpage | grep -v .svn | xargs grep $@"
find: invalid predicate `-v'

That makes me think that the pipe is not being recognized. What can I do to get this to work?

share|improve this question

  • 1

    Long command pipelines in shell variables should make anyone nervous. What are you trying to do? I recommend taking some time to read and digest BashFAQ 50 before taking this approach any further.
    – jw013
    Jul 26 ’12 at 20:19

  • are you trying to execute the contents of a variable or assign the output of a command to a variable? Your question says the former, but your response to Tim’s answer says the latter.
    – cas
    Jul 26 ’12 at 22:25

up vote
4
down vote

favorite

up vote
4
down vote

favorite

I have a unix command in a variable, it looks like this:

cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep $@"
`$cmd`
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

When I try to execute the command $cmd in a bash script, it won’t work. However, when I copy and paste the exact same command, it does work. Can you let me know what I am doing wrong?

I have tried putting quotes around the path, same error occurs

cmd="find "/path/to/webpage" -type f | grep -v .svn | xargs grep $@"
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

When I remove the -type f parameter, I get this error:

cmd="find /path/to/webpage | grep -v .svn | xargs grep $@"
find: invalid predicate `-v'

That makes me think that the pipe is not being recognized. What can I do to get this to work?

share|improve this question

I have a unix command in a variable, it looks like this:

cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep $@"
`$cmd`
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

When I try to execute the command $cmd in a bash script, it won’t work. However, when I copy and paste the exact same command, it does work. Can you let me know what I am doing wrong?

I have tried putting quotes around the path, same error occurs

cmd="find "/path/to/webpage" -type f | grep -v .svn | xargs grep $@"
find: paths must precede expression
Usage: find [-H] [-L] [-P] [path...] [expression]

When I remove the -type f parameter, I get this error:

cmd="find /path/to/webpage | grep -v .svn | xargs grep $@"
find: invalid predicate `-v'

That makes me think that the pipe is not being recognized. What can I do to get this to work?

bash find

share|improve this question

share|improve this question

share|improve this question

share|improve this question

edited Mar 3 at 3:09

Jeff Schaller

37.2k1052121

37.2k1052121

asked Jul 26 ’12 at 19:28

Roy Rico

179117

179117

  • 1

    Long command pipelines in shell variables should make anyone nervous. What are you trying to do? I recommend taking some time to read and digest BashFAQ 50 before taking this approach any further.
    – jw013
    Jul 26 ’12 at 20:19

  • are you trying to execute the contents of a variable or assign the output of a command to a variable? Your question says the former, but your response to Tim’s answer says the latter.
    – cas
    Jul 26 ’12 at 22:25

  • 1

    Long command pipelines in shell variables should make anyone nervous. What are you trying to do? I recommend taking some time to read and digest BashFAQ 50 before taking this approach any further.
    – jw013
    Jul 26 ’12 at 20:19

  • are you trying to execute the contents of a variable or assign the output of a command to a variable? Your question says the former, but your response to Tim’s answer says the latter.
    – cas
    Jul 26 ’12 at 22:25

1

1

Long command pipelines in shell variables should make anyone nervous. What are you trying to do? I recommend taking some time to read and digest BashFAQ 50 before taking this approach any further.
– jw013
Jul 26 ’12 at 20:19

Long command pipelines in shell variables should make anyone nervous. What are you trying to do? I recommend taking some time to read and digest BashFAQ 50 before taking this approach any further.
– jw013
Jul 26 ’12 at 20:19

are you trying to execute the contents of a variable or assign the output of a command to a variable? Your question says the former, but your response to Tim’s answer says the latter.
– cas
Jul 26 ’12 at 22:25

are you trying to execute the contents of a variable or assign the output of a command to a variable? Your question says the former, but your response to Tim’s answer says the latter.
– cas
Jul 26 ’12 at 22:25

4 Answers
4

active

oldest

votes

up vote
7
down vote

Others have already explained what to do. Let me explain what’s happening here: the pipe character | doesn’t make a pipeline as the variable is expanded, but acts like a literal character. Therefore, find is executed with the following arguments:

{"/path/to/webpage", "-type", "f", "|", "grep", "-v", ".svn", "|", ...}

and it interprets the | as a path and complains that it should have appeared before the expression (-type f).

Another big mistake is that you’re using `$cmd` as the sole command line. If $cmd (i.e. find ...) succeeded and produced output like rm -rf /, it would be executed on your behalf. Always take caution when you take data as code!

Improvement 1. find ... | grep -v ... is a poor way to exclude something from the output: find will traverse whole subdirectories named .svn, produce the lines, only to be thrown away later. Why not tell find to do it directly?

find path -type f | grep -v .svn                # don't do this
find path -name .svn -prune -o -type f -print   # do this instead

Improvement 2. When combining find and xargs, always use -print0 in find and -0 in xargs:

find path ... -print0 | xargs -0 -r grep ...    # I'd also recommend -r

or you can do it entirely in grep:

grep --recursive --exclude-dir=.svn pattern path

share|improve this answer

    up vote
    4
    down vote

    If you really want to execute code in a variable, you can do it using eval.

    cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep something"
    eval "$cmd"
    

    But since you’re trying to pass arguments in with $@, what you need here is a function

    webgrep() {
        find /path/to/webpage -type f | grep -v .svn | xargs grep "$@"
    }
    

    Note that this will have problems with any paths that include a whitespace character. Or patterns that start with a minus. And it will have to scan all the contents of .svn directories before ignoring them. And it would be nice to handle the user accidentally passing multiple arguments (e.g. because the pattern wasn’t quoted properly). A better way is

    webgrep() {
        find /path/to/webpage -name .svn -prune -o -type f -exec grep -e "$*" {} +
    }
    

    Then call it like this

    webgrep PATTERN
    

    share|improve this answer

    • You probably need to use grep -e "$*" to demote the list of command line arguments to a string and to protect against the cases where the string starts with a dash.
      – Kusalananda
      Jul 4 at 10:33

    • @Kusalananda Quite right, I’ve updated the last version of webgrep to address those concerns too.
      – Mikel
      Jul 4 at 18:36

    up vote
    0
    down vote

    Try building your variable like this:

    cmd=$(find /path/to/webpage -type f | grep -v .svn | xargs grep $@)
    

    or

    cmd=`find /path/to/webpage -type f | grep -v .svn | xargs grep $@`
    

    Or maybe an alias would be better suited:

    alias cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep $@"
    

    share|improve this answer

    • Thank you, using the $(command) method is what did the trick!
      – Roy Rico
      Jul 26 ’12 at 19:38

    • No problem! You caught me mid-edit, but should be fixed now.
      – Tim
      Jul 26 ’12 at 19:40

    • 5

      That’s not putting the code in a variable. It’s putting the output of the command in a variable.
      – Mikel
      Jul 26 ’12 at 21:27

    up vote
    0
    down vote

    Don’t put code in a variable, as a rule of thumb. Use a function.

    cmd() { 
      find /path/to/webpage -type f |
        grep -v .svn                | 
        xargs grep $@
    }
    
    cmd "$@"
    

    If you do eventually end up having to put simple code in a variable (don’t, but if you do…) then as mentioned, you can’t embed things like pipes or redirections without an eval…and don’t do that either, lol

    But don’t execute it in backticks, which run the commands in a subshell and replace it with the output, so that the output becomes what the parser is receiving as the command to execute:

    $: `echo foo`
    bash: foo: command not found
    

    share|improve this answer

      Your Answer

      StackExchange.ready(function() {
      var channelOptions = {
      tags: “”.split(” “),
      id: “106”
      };
      initTagRenderer(“”.split(” “), “”.split(” “), channelOptions);

      StackExchange.using(“externalEditor”, function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using(“snippets”, function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: ‘answer’,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: “”,
      imageUploader: {
      brandingHtml: “Powered by u003ca class=”icon-imgur-white” href=”https://imgur.com/”u003eu003c/au003e”,
      contentPolicyHtml: “User contributions licensed under u003ca href=”https://creativecommons.org/licenses/by-sa/3.0/”u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href=”https://stackoverflow.com/legal/content-policy”u003e(content policy)u003c/au003e”,
      allowUrls: true
      },
      onDemand: true,
      discardSelector: “.discard-answer”
      ,immediatelyShowMarkdownHelp:true
      });

      }
      });

      draft saved
      draft discarded

      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin(‘.new-post-login’, ‘https%3a%2f%2funix.stackexchange.com%2fquestions%2f44068%2fuse-a-shell-variable-to-execute-a-command%23new-answer’, ‘question_page’);
      }
      );

      Post as a guest

      Required, but never shown

      4 Answers
      4

      active

      oldest

      votes

      4 Answers
      4

      active

      oldest

      votes

      active

      oldest

      votes

      active

      oldest

      votes

      up vote
      7
      down vote

      Others have already explained what to do. Let me explain what’s happening here: the pipe character | doesn’t make a pipeline as the variable is expanded, but acts like a literal character. Therefore, find is executed with the following arguments:

      {"/path/to/webpage", "-type", "f", "|", "grep", "-v", ".svn", "|", ...}
      

      and it interprets the | as a path and complains that it should have appeared before the expression (-type f).

      Another big mistake is that you’re using `$cmd` as the sole command line. If $cmd (i.e. find ...) succeeded and produced output like rm -rf /, it would be executed on your behalf. Always take caution when you take data as code!

      Improvement 1. find ... | grep -v ... is a poor way to exclude something from the output: find will traverse whole subdirectories named .svn, produce the lines, only to be thrown away later. Why not tell find to do it directly?

      find path -type f | grep -v .svn                # don't do this
      find path -name .svn -prune -o -type f -print   # do this instead
      

      Improvement 2. When combining find and xargs, always use -print0 in find and -0 in xargs:

      find path ... -print0 | xargs -0 -r grep ...    # I'd also recommend -r
      

      or you can do it entirely in grep:

      grep --recursive --exclude-dir=.svn pattern path
      

      share|improve this answer

        up vote
        7
        down vote

        Others have already explained what to do. Let me explain what’s happening here: the pipe character | doesn’t make a pipeline as the variable is expanded, but acts like a literal character. Therefore, find is executed with the following arguments:

        {"/path/to/webpage", "-type", "f", "|", "grep", "-v", ".svn", "|", ...}
        

        and it interprets the | as a path and complains that it should have appeared before the expression (-type f).

        Another big mistake is that you’re using `$cmd` as the sole command line. If $cmd (i.e. find ...) succeeded and produced output like rm -rf /, it would be executed on your behalf. Always take caution when you take data as code!

        Improvement 1. find ... | grep -v ... is a poor way to exclude something from the output: find will traverse whole subdirectories named .svn, produce the lines, only to be thrown away later. Why not tell find to do it directly?

        find path -type f | grep -v .svn                # don't do this
        find path -name .svn -prune -o -type f -print   # do this instead
        

        Improvement 2. When combining find and xargs, always use -print0 in find and -0 in xargs:

        find path ... -print0 | xargs -0 -r grep ...    # I'd also recommend -r
        

        or you can do it entirely in grep:

        grep --recursive --exclude-dir=.svn pattern path
        

        share|improve this answer

          up vote
          7
          down vote

          up vote
          7
          down vote

          Others have already explained what to do. Let me explain what’s happening here: the pipe character | doesn’t make a pipeline as the variable is expanded, but acts like a literal character. Therefore, find is executed with the following arguments:

          {"/path/to/webpage", "-type", "f", "|", "grep", "-v", ".svn", "|", ...}
          

          and it interprets the | as a path and complains that it should have appeared before the expression (-type f).

          Another big mistake is that you’re using `$cmd` as the sole command line. If $cmd (i.e. find ...) succeeded and produced output like rm -rf /, it would be executed on your behalf. Always take caution when you take data as code!

          Improvement 1. find ... | grep -v ... is a poor way to exclude something from the output: find will traverse whole subdirectories named .svn, produce the lines, only to be thrown away later. Why not tell find to do it directly?

          find path -type f | grep -v .svn                # don't do this
          find path -name .svn -prune -o -type f -print   # do this instead
          

          Improvement 2. When combining find and xargs, always use -print0 in find and -0 in xargs:

          find path ... -print0 | xargs -0 -r grep ...    # I'd also recommend -r
          

          or you can do it entirely in grep:

          grep --recursive --exclude-dir=.svn pattern path
          

          share|improve this answer

          Others have already explained what to do. Let me explain what’s happening here: the pipe character | doesn’t make a pipeline as the variable is expanded, but acts like a literal character. Therefore, find is executed with the following arguments:

          {"/path/to/webpage", "-type", "f", "|", "grep", "-v", ".svn", "|", ...}
          

          and it interprets the | as a path and complains that it should have appeared before the expression (-type f).

          Another big mistake is that you’re using `$cmd` as the sole command line. If $cmd (i.e. find ...) succeeded and produced output like rm -rf /, it would be executed on your behalf. Always take caution when you take data as code!

          Improvement 1. find ... | grep -v ... is a poor way to exclude something from the output: find will traverse whole subdirectories named .svn, produce the lines, only to be thrown away later. Why not tell find to do it directly?

          find path -type f | grep -v .svn                # don't do this
          find path -name .svn -prune -o -type f -print   # do this instead
          

          Improvement 2. When combining find and xargs, always use -print0 in find and -0 in xargs:

          find path ... -print0 | xargs -0 -r grep ...    # I'd also recommend -r
          

          or you can do it entirely in grep:

          grep --recursive --exclude-dir=.svn pattern path
          

          share|improve this answer

          share|improve this answer

          share|improve this answer

          answered Jul 26 ’12 at 22:50

          musiphil

          6282613

          6282613

              up vote
              4
              down vote

              If you really want to execute code in a variable, you can do it using eval.

              cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep something"
              eval "$cmd"
              

              But since you’re trying to pass arguments in with $@, what you need here is a function

              webgrep() {
                  find /path/to/webpage -type f | grep -v .svn | xargs grep "$@"
              }
              

              Note that this will have problems with any paths that include a whitespace character. Or patterns that start with a minus. And it will have to scan all the contents of .svn directories before ignoring them. And it would be nice to handle the user accidentally passing multiple arguments (e.g. because the pattern wasn’t quoted properly). A better way is

              webgrep() {
                  find /path/to/webpage -name .svn -prune -o -type f -exec grep -e "$*" {} +
              }
              

              Then call it like this

              webgrep PATTERN
              

              share|improve this answer

              • You probably need to use grep -e "$*" to demote the list of command line arguments to a string and to protect against the cases where the string starts with a dash.
                – Kusalananda
                Jul 4 at 10:33

              • @Kusalananda Quite right, I’ve updated the last version of webgrep to address those concerns too.
                – Mikel
                Jul 4 at 18:36

              up vote
              4
              down vote

              If you really want to execute code in a variable, you can do it using eval.

              cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep something"
              eval "$cmd"
              

              But since you’re trying to pass arguments in with $@, what you need here is a function

              webgrep() {
                  find /path/to/webpage -type f | grep -v .svn | xargs grep "$@"
              }
              

              Note that this will have problems with any paths that include a whitespace character. Or patterns that start with a minus. And it will have to scan all the contents of .svn directories before ignoring them. And it would be nice to handle the user accidentally passing multiple arguments (e.g. because the pattern wasn’t quoted properly). A better way is

              webgrep() {
                  find /path/to/webpage -name .svn -prune -o -type f -exec grep -e "$*" {} +
              }
              

              Then call it like this

              webgrep PATTERN
              

              share|improve this answer

              • You probably need to use grep -e "$*" to demote the list of command line arguments to a string and to protect against the cases where the string starts with a dash.
                – Kusalananda
                Jul 4 at 10:33

              • @Kusalananda Quite right, I’ve updated the last version of webgrep to address those concerns too.
                – Mikel
                Jul 4 at 18:36

              up vote
              4
              down vote

              up vote
              4
              down vote

              If you really want to execute code in a variable, you can do it using eval.

              cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep something"
              eval "$cmd"
              

              But since you’re trying to pass arguments in with $@, what you need here is a function

              webgrep() {
                  find /path/to/webpage -type f | grep -v .svn | xargs grep "$@"
              }
              

              Note that this will have problems with any paths that include a whitespace character. Or patterns that start with a minus. And it will have to scan all the contents of .svn directories before ignoring them. And it would be nice to handle the user accidentally passing multiple arguments (e.g. because the pattern wasn’t quoted properly). A better way is

              webgrep() {
                  find /path/to/webpage -name .svn -prune -o -type f -exec grep -e "$*" {} +
              }
              

              Then call it like this

              webgrep PATTERN
              

              share|improve this answer

              If you really want to execute code in a variable, you can do it using eval.

              cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep something"
              eval "$cmd"
              

              But since you’re trying to pass arguments in with $@, what you need here is a function

              webgrep() {
                  find /path/to/webpage -type f | grep -v .svn | xargs grep "$@"
              }
              

              Note that this will have problems with any paths that include a whitespace character. Or patterns that start with a minus. And it will have to scan all the contents of .svn directories before ignoring them. And it would be nice to handle the user accidentally passing multiple arguments (e.g. because the pattern wasn’t quoted properly). A better way is

              webgrep() {
                  find /path/to/webpage -name .svn -prune -o -type f -exec grep -e "$*" {} +
              }
              

              Then call it like this

              webgrep PATTERN
              

              share|improve this answer

              share|improve this answer

              share|improve this answer

              edited Jul 4 at 18:33

              answered Jul 26 ’12 at 21:35

              Mikel

              38.7k1098125

              38.7k1098125

              • You probably need to use grep -e "$*" to demote the list of command line arguments to a string and to protect against the cases where the string starts with a dash.
                – Kusalananda
                Jul 4 at 10:33

              • @Kusalananda Quite right, I’ve updated the last version of webgrep to address those concerns too.
                – Mikel
                Jul 4 at 18:36

              • You probably need to use grep -e "$*" to demote the list of command line arguments to a string and to protect against the cases where the string starts with a dash.
                – Kusalananda
                Jul 4 at 10:33

              • @Kusalananda Quite right, I’ve updated the last version of webgrep to address those concerns too.
                – Mikel
                Jul 4 at 18:36

              You probably need to use grep -e "$*" to demote the list of command line arguments to a string and to protect against the cases where the string starts with a dash.
              – Kusalananda
              Jul 4 at 10:33

              You probably need to use grep -e "$*" to demote the list of command line arguments to a string and to protect against the cases where the string starts with a dash.
              – Kusalananda
              Jul 4 at 10:33

              @Kusalananda Quite right, I’ve updated the last version of webgrep to address those concerns too.
              – Mikel
              Jul 4 at 18:36

              @Kusalananda Quite right, I’ve updated the last version of webgrep to address those concerns too.
              – Mikel
              Jul 4 at 18:36

              up vote
              0
              down vote

              Try building your variable like this:

              cmd=$(find /path/to/webpage -type f | grep -v .svn | xargs grep $@)
              

              or

              cmd=`find /path/to/webpage -type f | grep -v .svn | xargs grep $@`
              

              Or maybe an alias would be better suited:

              alias cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep $@"
              

              share|improve this answer

              • Thank you, using the $(command) method is what did the trick!
                – Roy Rico
                Jul 26 ’12 at 19:38

              • No problem! You caught me mid-edit, but should be fixed now.
                – Tim
                Jul 26 ’12 at 19:40

              • 5

                That’s not putting the code in a variable. It’s putting the output of the command in a variable.
                – Mikel
                Jul 26 ’12 at 21:27

              up vote
              0
              down vote

              Try building your variable like this:

              cmd=$(find /path/to/webpage -type f | grep -v .svn | xargs grep $@)
              

              or

              cmd=`find /path/to/webpage -type f | grep -v .svn | xargs grep $@`
              

              Or maybe an alias would be better suited:

              alias cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep $@"
              

              share|improve this answer

              • Thank you, using the $(command) method is what did the trick!
                – Roy Rico
                Jul 26 ’12 at 19:38

              • No problem! You caught me mid-edit, but should be fixed now.
                – Tim
                Jul 26 ’12 at 19:40

              • 5

                That’s not putting the code in a variable. It’s putting the output of the command in a variable.
                – Mikel
                Jul 26 ’12 at 21:27

              up vote
              0
              down vote

              up vote
              0
              down vote

              Try building your variable like this:

              cmd=$(find /path/to/webpage -type f | grep -v .svn | xargs grep $@)
              

              or

              cmd=`find /path/to/webpage -type f | grep -v .svn | xargs grep $@`
              

              Or maybe an alias would be better suited:

              alias cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep $@"
              

              share|improve this answer

              Try building your variable like this:

              cmd=$(find /path/to/webpage -type f | grep -v .svn | xargs grep $@)
              

              or

              cmd=`find /path/to/webpage -type f | grep -v .svn | xargs grep $@`
              

              Or maybe an alias would be better suited:

              alias cmd="find /path/to/webpage -type f | grep -v .svn | xargs grep $@"
              

              share|improve this answer

              share|improve this answer

              share|improve this answer

              edited Jul 26 ’12 at 19:37

              answered Jul 26 ’12 at 19:30

              Tim

              4,9901216

              4,9901216

              • Thank you, using the $(command) method is what did the trick!
                – Roy Rico
                Jul 26 ’12 at 19:38

              • No problem! You caught me mid-edit, but should be fixed now.
                – Tim
                Jul 26 ’12 at 19:40

              • 5

                That’s not putting the code in a variable. It’s putting the output of the command in a variable.
                – Mikel
                Jul 26 ’12 at 21:27

              • Thank you, using the $(command) method is what did the trick!
                – Roy Rico
                Jul 26 ’12 at 19:38

              • No problem! You caught me mid-edit, but should be fixed now.
                – Tim
                Jul 26 ’12 at 19:40

              • 5

                That’s not putting the code in a variable. It’s putting the output of the command in a variable.
                – Mikel
                Jul 26 ’12 at 21:27

              Thank you, using the $(command) method is what did the trick!
              – Roy Rico
              Jul 26 ’12 at 19:38

              Thank you, using the $(command) method is what did the trick!
              – Roy Rico
              Jul 26 ’12 at 19:38

              No problem! You caught me mid-edit, but should be fixed now.
              – Tim
              Jul 26 ’12 at 19:40

              No problem! You caught me mid-edit, but should be fixed now.
              – Tim
              Jul 26 ’12 at 19:40

              5

              5

              That’s not putting the code in a variable. It’s putting the output of the command in a variable.
              – Mikel
              Jul 26 ’12 at 21:27

              That’s not putting the code in a variable. It’s putting the output of the command in a variable.
              – Mikel
              Jul 26 ’12 at 21:27

              up vote
              0
              down vote

              Don’t put code in a variable, as a rule of thumb. Use a function.

              cmd() { 
                find /path/to/webpage -type f |
                  grep -v .svn                | 
                  xargs grep $@
              }
              
              cmd "$@"
              

              If you do eventually end up having to put simple code in a variable (don’t, but if you do…) then as mentioned, you can’t embed things like pipes or redirections without an eval…and don’t do that either, lol

              But don’t execute it in backticks, which run the commands in a subshell and replace it with the output, so that the output becomes what the parser is receiving as the command to execute:

              $: `echo foo`
              bash: foo: command not found
              

              share|improve this answer

                up vote
                0
                down vote

                Don’t put code in a variable, as a rule of thumb. Use a function.

                cmd() { 
                  find /path/to/webpage -type f |
                    grep -v .svn                | 
                    xargs grep $@
                }
                
                cmd "$@"
                

                If you do eventually end up having to put simple code in a variable (don’t, but if you do…) then as mentioned, you can’t embed things like pipes or redirections without an eval…and don’t do that either, lol

                But don’t execute it in backticks, which run the commands in a subshell and replace it with the output, so that the output becomes what the parser is receiving as the command to execute:

                $: `echo foo`
                bash: foo: command not found
                

                share|improve this answer

                  up vote
                  0
                  down vote

                  up vote
                  0
                  down vote

                  Don’t put code in a variable, as a rule of thumb. Use a function.

                  cmd() { 
                    find /path/to/webpage -type f |
                      grep -v .svn                | 
                      xargs grep $@
                  }
                  
                  cmd "$@"
                  

                  If you do eventually end up having to put simple code in a variable (don’t, but if you do…) then as mentioned, you can’t embed things like pipes or redirections without an eval…and don’t do that either, lol

                  But don’t execute it in backticks, which run the commands in a subshell and replace it with the output, so that the output becomes what the parser is receiving as the command to execute:

                  $: `echo foo`
                  bash: foo: command not found
                  

                  share|improve this answer

                  Don’t put code in a variable, as a rule of thumb. Use a function.

                  cmd() { 
                    find /path/to/webpage -type f |
                      grep -v .svn                | 
                      xargs grep $@
                  }
                  
                  cmd "$@"
                  

                  If you do eventually end up having to put simple code in a variable (don’t, but if you do…) then as mentioned, you can’t embed things like pipes or redirections without an eval…and don’t do that either, lol

                  But don’t execute it in backticks, which run the commands in a subshell and replace it with the output, so that the output becomes what the parser is receiving as the command to execute:

                  $: `echo foo`
                  bash: foo: command not found
                  

                  share|improve this answer

                  share|improve this answer

                  share|improve this answer

                  answered Nov 28 at 19:46

                  Paul Hodges

                  1663

                  1663

                      draft saved
                      draft discarded

                      Thanks for contributing an answer to Unix & Linux Stack Exchange!

                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid

                      • Asking for help, clarification, or responding to other answers.
                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.

                      Some of your past answers have not been well-received, and you’re in danger of being blocked from answering.

                      Please pay close attention to the following guidance:

                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid

                      • Asking for help, clarification, or responding to other answers.
                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.

                      draft saved

                      draft discarded

                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin(‘.new-post-login’, ‘https%3a%2f%2funix.stackexchange.com%2fquestions%2f44068%2fuse-a-shell-variable-to-execute-a-command%23new-answer’, ‘question_page’);
                      }
                      );

                      Post as a guest

                      Required, but never shown

                      Required, but never shown

                      Required, but never shown

                      Required, but never shown

                      Required, but never shown

                      Required, but never shown

                      Required, but never shown

                      Required, but never shown

                      Required, but never shown

                      Related Post

                      Leave a Reply

                      Your email address will not be published. Required fields are marked *