Cthulhux
Cthulhux

Reputation: 409

error with an alias in .zshrc file

I have a problem with en alias in my .zshrc file in macOS. Here is the alias :

alias update='sudo softwareupdate -i -a; brew update; for i in $(brew cask outdated --quiet); do brew cask reinstall $i; done; brew cleanup -r; gem update --system; gem update; upgrade_oh_my_zsh; npm update -g; for x in $(pip3 list -o --format=columns | sed -n '3,$p' | cut -d' ' -f1); do pip3 install $x --upgrade; done';

When i call it, the shell answers me :

-for cmdsubst>

I can not find the origin of the problem.

Upvotes: 0

Views: 292

Answers (2)

Adaephon
Adaephon

Reputation: 18339

Like piojo wrote the issue is that it is not possible to use single quotes within single quotes.

In this case, instead of working around the quoting, I would recommend to create a function instead of an alias.

update () {
    sudo softwareupdate -i -a
    brew update
    for i in $(brew cask outdated --quiet); do 
        brew cask reinstall $i
    done
    brew cleanup -r
    gem update --system
    gem update
    upgrade_oh_my_zsh
    npm update -g
    for x in $(pip3 list -o --format=columns | 
               sed -n '3,$p' | cut -d' ' -f1); do
        pip3 install $x --upgrade
    done
}

That way you need only the quotes you would need on the command line anyway. I think it also improves readability and makes edits easier. (Granted, it is also possible to have multi-line alias definitions)

Upvotes: 1

piojo
piojo

Reputation: 6723

The crux of the issue looks to be that you're using single quotes to define the alias, but you're also trying to use single quotes within it. This breaks the continuous string zsh needs to see when you define alias foo=string. You can use a single quote within a single quoted string by ending it, escaping the quote, and starting anew. For example:

alias foo='command '\''$1'\'' another-arg'

Or to include a quote at the end, just end the quoted string and escape just a single quote:

echo 'this is a single quote: '\'

Upvotes: 1

Related Questions