Difference between ampersand (&) before or after command arguments

1

I am a Windows user and I am using Cygwin as my terminal. In order to run notepad++ from terminal I have added this lines to ~/.bash_profile:

npp () {
    /cygdrive/d/Notepad++/notepad++.exe $(cygpath -w -- "$@")
    }

it worked, so next I needed a way to launch it on the background so I could open a npp++ instance without stopping my terminal flow. This worked:

npp foo &

But I did not want to introduce that ampersand manually, so I tried adding this alias to ~/.bash_profile:

alias npp="npp &"

And now:

npp foo

Works in the background.

The problem is that with gnu/linux commands you need to write ampersand after the arguments, if not it will not work.

this works:

ls directory &

This doesnt:

ls & directory

So I would like to know what am I missing here, because:

npp foo

Actually works in the background when I set

alias npp="npp &"

And it does not when I comment it.

adriloma

Posted 2016-03-13T10:43:05.187

Reputation: 13

Answers

0

The following command is launching in the background the ls executable passing directory as its single argument, this is what you want to do:

ls directory &

That one is launching ls in the background without any argument and then is (trying to) launch directory as a command in the foreground, this is not what you want to do:

ls & directory

Using an alias doesn't change this behavior, unless cygwin sh has a specific feature I'm unaware of, which I strongly doubt.

jlliagre

Posted 2016-03-13T10:43:05.187

Reputation: 12 469

You are right, I have tested that 'ls & directory' just list current directory and then throws an error for missing 'directory' command. So, why 'npp foo' works as 'npp foo &' when 'npp' is an alias to 'npp &'? – adriloma – 2016-03-13T10:51:37.250

It doesn't work with bash under Linux, I didn't test under cygwin but I would be very surprised if it behaves differently. – jlliagre – 2016-03-13T10:55:23.417

thank you, I was missing something: I run notepad++ without -nosession and -multiinst options, so whenever it is opened it remembers last session files. As I have always been testing it with the same file, it was always loaded, so 'npp foo' was already 'npp & foo', which ignores 'foo', but foo appears on notepad++ because it was loaded on last session. what a misunderstood, thanks! – adriloma – 2016-03-13T11:15:28.797