Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Linux Programming
  4. execute multiple commands assigned to a single variable - bash

execute multiple commands assigned to a single variable - bash

Scheduled Pinned Locked Moved Linux Programming
linuxhelp
3 Posts 3 Posters 10 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    raphael Oct2022
    wrote on last edited by
    #1

    Hi, I would like to ask the user to enter a command that will be executed, but I don't understand why I can't assign two commands to the same variable. Here is the code:

    user@localhost:~# read -ep "command: " cmd ; "$cmd"

    And result:

    command: id ; date
    -bash: id ; date : command not found

    but if I type a single command, it works. Thanks for your help

    K 1 Reply Last reply
    0
    • R raphael Oct2022

      Hi, I would like to ask the user to enter a command that will be executed, but I don't understand why I can't assign two commands to the same variable. Here is the code:

      user@localhost:~# read -ep "command: " cmd ; "$cmd"

      And result:

      command: id ; date
      -bash: id ; date : command not found

      but if I type a single command, it works. Thanks for your help

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      When the user types in id; date, that's the value of the $cmd variable. So when you want to execute the command, its as if you had typed "id; date" at the command line, including the quote marks. So the bash interpreter is looking for a command named id; date. Its not treating this as "subscript" to be parsed and executed. For that you'll need to use eval

      [k5054@localhost ~]$ cmd="id; date"
      [k5054@localhost ~]$ $cmd
      -bash: id;: command not found
      [k5054@localhost ~]$ eval $cmd
      uid=1002(k5054) gid=1002(k5054) groups=1002(k5054)
      Mon 17 Oct 2022 02:37:19 PM UTC
      [k5054@localhost ~]$

      Keep Calm and Carry On

      L 1 Reply Last reply
      0
      • K k5054

        When the user types in id; date, that's the value of the $cmd variable. So when you want to execute the command, its as if you had typed "id; date" at the command line, including the quote marks. So the bash interpreter is looking for a command named id; date. Its not treating this as "subscript" to be parsed and executed. For that you'll need to use eval

        [k5054@localhost ~]$ cmd="id; date"
        [k5054@localhost ~]$ $cmd
        -bash: id;: command not found
        [k5054@localhost ~]$ eval $cmd
        uid=1002(k5054) gid=1002(k5054) groups=1002(k5054)
        Mon 17 Oct 2022 02:37:19 PM UTC
        [k5054@localhost ~]$

        Keep Calm and Carry On

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I spent an hour earlier trying to figure out a way to do this. I need get my Unix books out of storage.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups