Results 1 to 6 of 6

Thread: Bash Commands in Variables

  1. #1
    Join Date
    Oct 2006
    Location
    St. Louis
    Beans
    52
    Distro
    Ubuntu 11.04 Natty Narwhal

    Bash Commands in Variables

    I'm being stupid here. Maybe just tired. Anyone help?

    Code:
    aaron@corona ~/code$ cat up
    #! /bin/bash
    COMMAND='sudo aptitude update && sudo aptitude safe-upgrade'
    echo "$COMMAND"
    $COMMAND
    I'm sure it's something simple, it's just not coming to me and Googling hasn't been fruitful.

    Results in:
    Code:
    aaron@corona ~/code$ up
    sudo aptitude update && sudo aptitude safe-upgrade
    /usr/local/bin/up: line 4: sudo aptitude update && sudo aptitude safe-upgrade: command not found

  2. #2
    Join Date
    Oct 2006
    Location
    St. Louis
    Beans
    52
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Bash Commands in Variables

    Okay. Double quotes mostly works.

    Code:
    aaron@corona ~/code$ cat up
    #! /bin/bash
    COMMAND="sudo aptitude update && sudo aptitude safe-upgrade"
    echo "$COMMAND"
    $COMMAND
    But it still doesn't like the "&&". Anyone?

    Code:
    aaron@corona ~$ up
    sudo aptitude update && sudo aptitude safe-upgrade
    E: The update command takes no arguments

  3. #3
    Join Date
    Mar 2008
    Location
    Birmingham, UK
    Beans
    Hidden!

    Re: Bash Commands in Variables

    I'm not sure about the bash command in a variable, but you could achieve the same using an alias. Edit ~/.bashrc and add:
    Code:
    alias up='sudo apt-get update && sudo apt-get upgrade'
    Then source ~/.bashrc so it updates the alias
    Code:
    source ~/.bashrc
    From then on you should be able to call up and it will run that command.

    Apologies if you were just wanting to be able to do it in bash - I can't help in that respect.
    Last edited by m_duck; October 17th, 2009 at 05:33 PM.
    Desktop: Phenom 955 BE | GA-MA790XT-UD4P | 8GB TG Elite 1600 | BFG GTX 275
    Conky Screenshots | Last.fm | New to Ubuntu?

  4. #4
    Join Date
    Oct 2006
    Location
    St. Louis
    Beans
    52
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Bash Commands in Variables

    Thanks for the suggestion. I use several aliai () currently. This is kind of just an exercise and has a little more portability.

  5. #5
    Join Date
    Dec 2006
    Beans
    1,133
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash Commands in Variables

    Try

    eval $command

    In your example everything after the first "aptitude" command is being passed as arguements to "aptitude" rather than being evaluated by the shell. When "eval" is used all arguments are passed to the shell for evaluation.
    There are no dumb questions, just dumb answers.

  6. #6
    Join Date
    Oct 2006
    Location
    St. Louis
    Beans
    52
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: Bash Commands in Variables

    Quote Originally Posted by lswb View Post
    Try

    eval $command

    In your example everything after the first "aptitude" command is being passed as arguements to "aptitude" rather than being evaluated by the shell. When "eval" is used all arguments are passed to the shell for evaluation.
    Aha! Perfect! I was not familiar with that but solved it all. Thank you much!

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •