Results 1 to 9 of 9

Thread: Bash alias question

  1. #1
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    672
    Distro
    Ubuntu 14.04 Trusty Tahr

    Question Bash alias question

    Hi All,

    I have several aliases for commands I often use.

    Example
    Code:
    alias conkycalendar='conky -c ~/conkys/conkyrc2'
    They all work fine.

    I thought I'd put an alias in the .bash_aliases file to run a long command that shows reminders.

    This is the command that works in the terminal.
    Code:
    remind -a '-kgmessage -buttons "OK:1" -default "OK" -center -font "serif 16" -fg "#46f" -bg white -wrap -title "Remember" %s &' ~/.reminders-timed
    However if I set that with an alias of rt I get this:
    Code:
     bash:  ~/.reminders-timed: No such file or directory
    bash: alias: -buttons: not found
    bash: alias: OK:1: not found
    bash: alias: -default: not found
    bash: alias: OK: not found
    bash: alias: -center: not found
    bash: alias: -font: not found
    bash: alias: serif 16: not found
    bash: alias: -fg: not found
    bash: alias: #46f: not found
    bash: alias: -bg: not found
    bash: alias: white: not found
    bash: alias: -wrap: not found
    bash: alias: -title: not found
    bash: alias: Remember: not found
    bash: alias: %s: not found
    Does anyone know why the command will run in the terminal, but not when I add an alias to it?

    Update: I solved the problem by saving the command as a Bash script and setting the alias to the script. I still don't understand why that was necessary though.
    Last edited by GrouchyGaijin; January 14th, 2011 at 12:35 AM. Reason: found a work around
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  2. #2
    Join Date
    Sep 2008
    Location
    USA Pacific Coast
    Beans
    68
    Distro
    Ubuntu

    Re: Bash alias question

    Quote Originally Posted by GrouchyGaijin View Post
    Hi All,
    Update: I solved the problem by saving the command as a Bash script and setting the alias to the script. I still don't understand why that was necessary though.
    If you were using the command exactly as you wrote it above, I'm guessing you had your alias line written as

    Code:
    alias = 'remind -a '-kgmessage -buttons "OK:1" -default "OK" -center -font "serif 16" -fg "#46f" -bg white -wrap -title "Remember" %s &' ~/.reminders-timed'
    #       ^          ^
    #       |          |
    #       Problems start here.
    As you can see, the single quotes ( ' ) are being closed prematurely.

    Since the command you are using needs both single and double quotes, I think you're using the smart approach by just saving the command to a bash script file.
    To the optimist, the glass is half full.
    To the pessimist, the glass is half empty.
    To the engineer, the glass is twice as big as it needs to be.

  3. #3
    Join Date
    Apr 2006
    Location
    Montana
    Beans
    Hidden!
    Distro
    Kubuntu Development Release

    Re: Bash alias question

    That and IMO you should use the full path in scripts.

    So rather then ~ or ./ use

    /home/your_user/foo

    Same with commands.

    Use /bin/bar not bar

    For complex commands, rather then an alias, define a function.
    There are two mistakes one can make along the road to truth...not going all the way, and not starting.
    --Prince Gautama Siddharta

    #ubuntuforums web interface

  4. #4
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    672
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash alias question

    Quote Originally Posted by bodhi.zazen View Post
    That and IMO you should use the full path in scripts.

    So rather then ~ or ./ use

    /home/your_user/foo

    Same with commands.

    Use /bin/bar not bar

    For complex commands, rather then an alias, define a function.
    I'm kind of new to this. Can you explain why it is better to use the full path?
    I'm not sure what a function is either. How would I make one and why is that better than an alias?
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  5. #5
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,301
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Bash alias question

    full path is foolproof and always works

    short example of shell function
    http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html
    aliases work as a simple string substitution and are pretty much for oneliners only plus you get the parsing problems with ' and ", functions allow for more advanced constructs
    Last edited by Vaphell; January 14th, 2011 at 09:31 AM.

  6. #6
    Join Date
    Apr 2006
    Location
    Montana
    Beans
    Hidden!
    Distro
    Kubuntu Development Release

    Re: Bash alias question

    Quote Originally Posted by Vaphell View Post
    full path is foolproof and always works

    short example of shell function
    http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html
    aliases work as a simple string substitution and are pretty much for oneliners only plus you get the parsing problems with ' and ", functions allow for more advanced constructs
    Because variables such as ~ , ./ are inconsistent. What happens when user B runs the script ? What about when root runs it ? What happens when you call it from ~ ? What about from ~/Desktop ?

    See also (bash) environmental variables.

    It is much better to hard code.

    If you call something more then once, use a variable

    IPTABLES=/sbin/iptables

    $IPTABLES ....
    $IPTABLES ....
    There are two mistakes one can make along the road to truth...not going all the way, and not starting.
    --Prince Gautama Siddharta

    #ubuntuforums web interface

  7. #7
    Join Date
    Oct 2010
    Location
    Sweden
    Beans
    672
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Bash alias question

    Thanks guys,

    From now on I'll use the full path.
    Since I'm the only one who uses my laptop I never even considered a "user B" running a script I have.

    I read through the link that was posted about functions and honestly I found his explanation really confusing. I did find another beginners guide to BASH online that seems to be a bit more clearly written. (Perhaps the first guide was also clearly written for someone with more background knowledge than I have.)
    Thank you,
    GG -----------
    GUI we don't need no stinking GUI: A collection of Bash scripts
    Mutt (email), cmus (music),Google Calendar and Google Tasks cli

  8. #8
    Join Date
    Apr 2006
    Location
    Montana
    Beans
    Hidden!
    Distro
    Kubuntu Development Release

    Re: Bash alias question

    Quote Originally Posted by GrouchyGaijin View Post
    Thanks guys,

    From now on I'll use the full path.
    Since I'm the only one who uses my laptop I never even considered a "user B" running a script I have.

    I read through the link that was posted about functions and honestly I found his explanation really confusing. I did find another beginners guide to BASH online that seems to be a bit more clearly written. (Perhaps the first guide was also clearly written for someone with more background knowledge than I have.)
    Functions are nothing much more then complex aliases.

    You define them in .bashrc, the syntax is a bit different, but basically they are short scripts.
    There are two mistakes one can make along the road to truth...not going all the way, and not starting.
    --Prince Gautama Siddharta

    #ubuntuforums web interface

  9. #9
    Join Date
    Feb 2007
    Location
    Romania
    Beans
    Hidden!
    Distro
    Ubuntu Development Release

    Re: Bash alias question

    Quote Originally Posted by GrouchyGaijin View Post
    I read through the link that was posted about functions and honestly I found his explanation really confusing. I did find another beginners guide to BASH online that seems to be a bit more clearly written. (Perhaps the first guide was also clearly written for someone with more background knowledge than I have.)
    That howto is outdated (last updated at Thu Jul 27 09:36:18 ART 2000).

    There is another Bash howto on that site, the Advanced Bash-Scripting Guide. I wouldn't recommend it either. It teaches you to write bugs, not scripts.

    If you are looking for a good Bash howto, check out http://mywiki.wooledge.org/BashGuide
    Last edited by sisco311; January 15th, 2011 at 01:45 AM.

Tags for this Thread

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
  •