1

I am running a command as such:

submit -n 72 -x '--mail-type=END -N2'

This command works. However, when I set it as an alias:

alias foo='submit -n 72 -x '--mail-type=END -N2''

it fails with the error:

bash: alias: -N2: not found

However, this alias works just fine:

alias foo='submit -n 72 -x '-N2''

So there is something specifically about the --mail-type=END snippet that causes problems.

EDIT: Changed alias='... to alias foo='...

3
  • In short: quotes don't nest. The second ' ends the quoting, so there's an unquoted space between the END and -N2. The final two quotes quote an empty string, which gets concatenated to the -N2, so they don't do anything.
    – ilkkachu
    Apr 19, 2017 at 21:54
  • That's not a valid alias declaration. Do you actually have something like alias foo='submit -n 72 -x '--mail-type=END -N2'' or are you in actual fact attempting to assign this string to a variable named alias?
    – tripleee
    Apr 20, 2017 at 3:45
  • Yes, I realize now that I typed it wrong in the post but in real life it's alias foo='submit -n 72 -x '-N2'' Apr 20, 2017 at 16:15

2 Answers 2

3

The quoting is off. Instead try using "" to contain '' (though "" interpolates $variables, which you may or may not want)

alias="submit -n 72 -x '--mail-type=END -N2'"

Or use the '\'' trick to "end the current single quote, insert a literal quote, restart the current single quote". That is, any inner ' is replaced with '\''

alias='submit -n 72 -x '\''--mail-type=END -N2'\'''

Here's how I verified whether the quoting produced a single unbroken string and not two words like yours does:

# not ok, two distinct words
$ perl -E 'say for @ARGV' 'submit -n 72 -x '--mail-type=END -N2''
submit -n 72 -x --mail-type=END
-N2

# ok, a single string
$ perl -E 'say for @ARGV' 'submit -n 72 -x '-N2''                
submit -n 72 -x -N2
$ 

# ok, single quotes within doubles
$ perl -E 'say for @ARGV' "submit -n 72 -x '--mail-type=END -N2'"
submit -n 72 -x '--mail-type=END -N2'

# ok, '\'' trick
$ perl -E 'say for @ARGV' 'submit -n 72 -x '\''--mail-type=END -N2'\'''
submit -n 72 -x '--mail-type=END -N2'
$ 

As for foo='submit -n 72 -x '-N2'', that is not nested; quotes on unix do not nest, this is why shells have $(cmd ... $(cmd ...)) instead of trying to figure out how to nest that using only backticks. foo='submit -n 72 -x '-N2'' is a more verbose and complicated way to say foo='submit -n 72 -x '-N2 or even more simply foo='submit -n 72 -x -N2'.

2
  • This works! But I don't understand why alias foo='submit -n 72 -x '-N2'' works also as I mentioned? It also has nested single quotes. Apr 20, 2017 at 16:16
  • They are not actually nested, they are adjacent. submit -n 72 -x (space) is quoted, and followed by an unquoted N2, which is followed by a quoted empty string.
    – tripleee
    Apr 20, 2017 at 16:25
1

You cannot nest quotes. A simple workaround is to use different quotes:

alias foo='submit -n 72 -x "--mail-type=END -N2"'

A much better workaround is to not use an alias. Use a function instead.

foo () {
    submit -n 72 -x '--mail-type=END -N2' "$@"
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .