You are not logged in.

#1 2013-01-26 11:17:35

nTia89
Banned
From: varese, italy
Registered: 2008-12-22
Posts: 1,230

differences between ' and "

i get a curious feedback from a command with apostrophes or quotation marks, so i want to understand more about their behaviour

executing:

alias log='journalctl --this-boot --no-pager > /tmp/log && $EDITOR /tmp/log'
log

i get geany as should because EDITOR=geany
but if i run:

alias log="journalctl --this-boot --no-pager > /tmp/log && $EDITOR /tmp/log"
log

i get

bash: /tmp/log: Permission denied

WHY ?

Last edited by nTia89 (2013-01-26 11:25:06)


+pc: custom | AMD Opteron 175 | nForce4 Ultra | 2GB ram DDR400 | nVidia 9800GT 1GB | ArchLinux x86_64 w/ openbox
+laptop: Apple | MacBook (2,1) | 2GB ram | Mac OS X 10.4 -> DIED
+ultrabook: Dell | XPS 13 (9343) | 8GB ram | 256GB ssd | FullHD display | Windows 8.1 64bit ArchLinux x86_64 w/ Gnome

Offline

#2 2013-01-26 11:19:06

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,595
Website

Re: differences between ' and "

' does not allow variable to expand to their targets but " does... do this simple experiment to see:

TEST="foo bar"
echo "$TEST"
echo '$TEST'

Last edited by graysky (2013-01-26 11:20:04)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#3 2013-01-26 11:28:02

nTia89
Banned
From: varese, italy
Registered: 2008-12-22
Posts: 1,230

Re: differences between ' and "

sorry, but I don't understand why I get different behaviour (i edited the first post, for completeness I run alias in the terminal); maybe now it's different

Last edited by nTia89 (2013-01-26 11:29:10)


+pc: custom | AMD Opteron 175 | nForce4 Ultra | 2GB ram DDR400 | nVidia 9800GT 1GB | ArchLinux x86_64 w/ openbox
+laptop: Apple | MacBook (2,1) | 2GB ram | Mac OS X 10.4 -> DIED
+ultrabook: Dell | XPS 13 (9343) | 8GB ram | 256GB ssd | FullHD display | Windows 8.1 64bit ArchLinux x86_64 w/ Gnome

Offline

#4 2013-01-26 12:05:03

msthev
Member
Registered: 2012-04-05
Posts: 177

Re: differences between ' and "

$ alias log='$EDITOR /tmp/log'
$ alias log
log='$EDITOR /tmp/log'
$ alias log="$EDITOR /tmp/log"                                                                                                                                                                                                                
$ alias log                                                                                                                                                                                                                                   
log='vim /tmp/log'

And one more time without $EDITOR set:

$ unset EDITOR
$ alias log='$EDITOR /tmp/log'                                                                                                                                                                                                                
$ alias log                                                                                                                                                                                                                                   
log='$EDITOR /tmp/log'
$ alias log="$EDITOR /tmp/log"                                                                                                                                                                                                                
$ alias log                                                                                                                                                                                                                                   
log=' /tmp/log'

And, as you might imagine, executing /tmp/log gives you permission denied.
The second version not working means you have your $EDITOR set incorrectly.

Offline

#5 2013-01-26 12:14:13

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,445
Website

Re: differences between ' and "

Just to build on the answer above, if you set EDITOR *before* defining those aliases, you will get the exact same behavior from both[1].  But I suspect you set EDITOR=geaney later in the bashrc (or other file it's in) after setting the aliases.  With single quotes the EDITOR variable is expanded each time the alias is used, with double quotes EDITOR is expanded only once when the alias is created.


[1]: they could actually slightly different behavior - the single quoted alias's behavior will "update" with any change to the EDITOR variable, the doubly quoted method's behavior is dictated by the value of EDITOR when the alias was created.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2013-01-26 12:45:37

nTia89
Banned
From: varese, italy
Registered: 2008-12-22
Posts: 1,230

Re: differences between ' and "

Trilby wrote:

Just to build on the answer above, if you set EDITOR *before* defining those aliases, you will get the exact same behavior from both[1].  But I suspect you set EDITOR=geaney later in the bashrc (or other file it's in) after setting the aliases.  With single quotes the EDITOR variable is expanded each time the alias is used, with double quotes EDITOR is expanded only once when the alias is created.


[1]: they could actually slightly different behavior - the single quoted alias's behavior will "update" with any change to the EDITOR variable, the doubly quoted method's behavior is dictated by the value of EDITOR when the alias was created.

yes, I set variables after aliases.....

so, apostrophe provides a dynamic alias, instead, quotation mark a static one?

Last edited by nTia89 (2013-01-26 12:46:44)


+pc: custom | AMD Opteron 175 | nForce4 Ultra | 2GB ram DDR400 | nVidia 9800GT 1GB | ArchLinux x86_64 w/ openbox
+laptop: Apple | MacBook (2,1) | 2GB ram | Mac OS X 10.4 -> DIED
+ultrabook: Dell | XPS 13 (9343) | 8GB ram | 256GB ssd | FullHD display | Windows 8.1 64bit ArchLinux x86_64 w/ Gnome

Offline

#7 2013-01-26 13:00:05

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,445
Website

Re: differences between ' and "

nTia89 wrote:

so, apostrophe provides a dynamic alias, instead, quotation mark a static one?

I'm not sure that that's a good way to think of it - primarily because that sounds backwards.  Just see Graysky's post above: variables are expanded within double quotes, but not within single quotes.  The "dynamic" part of the alias in your example comes from the fact that there is a variable in it so these distinctions apply.

In either case, the alias is defined precisely as what comes after the equals sign - there is no dynamic component.  But if what comes after the equals sign is a reference to a variable, the alias will always expand to the reference to the variable, which the shell will further expand when it tries to execute it.  If what comes after the equals sign is the current value of a variable, the alias will always expand to whatever that value was when it was set.

Last edited by Trilby (2013-01-26 13:03:01)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2013-01-26 13:27:02

nTia89
Banned
From: varese, italy
Registered: 2008-12-22
Posts: 1,230

Re: differences between ' and "

but actually the behaviour is the opposite: only with single quota, apostrophe the log works, the variable is evaluated....

for completeness:

#
# ~/.bashrc
#

...

# Aliases
alias nano='nano -S'
...
...
...
alias log='journalctl --this-boot --no-pager > /tmp/log && $EDITOR /tmp/log'
alias logall='journalctl --no-pager > /tmp/logall && $EDITOR /tmp/logall'

# Export variables
if [ -n '$DISPLAY' ]; then
        export BROWSER='firefox'
        export EDITOR='geany -s'
        export VISUAL='geany -s'
else
        export BROWSER='links'
        export EDITOR='nano'
        export VISUAL='nano'
fi

...
...
...

+pc: custom | AMD Opteron 175 | nForce4 Ultra | 2GB ram DDR400 | nVidia 9800GT 1GB | ArchLinux x86_64 w/ openbox
+laptop: Apple | MacBook (2,1) | 2GB ram | Mac OS X 10.4 -> DIED
+ultrabook: Dell | XPS 13 (9343) | 8GB ram | 256GB ssd | FullHD display | Windows 8.1 64bit ArchLinux x86_64 w/ Gnome

Offline

#9 2013-01-26 13:45:56

cmtptr
Member
Registered: 2008-09-01
Posts: 135

Re: differences between ' and "

nTia89, think of it this way.

1 $ EDITOR="editor-one"
2 $ alias log='journalctl --this-boot --no-pager > /tmp/log && $EDITOR /tmp/log'
3 $ EDITOR="editor-two"
4 $ log  # this is the same as typing "journalctl --this-boot --no-pager > /tmp/log && $EDITOR /tmp/log"; at this point, $EDITOR will be expanded to "editor-two"

In this example, the value of the alias "log" is the literal string "journalctl --this-boot --no-pager > /tmp/log && $EDITOR /tmp/log", including the dollar sign and the letters E, D, I, T, O, and R.

1 $ EDITOR="editor-one"
2 $ alias log="journalctl --this-boot --no-pager > /tmp/log && $EDITOR /tmp/log"
3 $ EDITOR="editor-two"
4 $ log  # this is the same as typing "journalctl --this-boot --no-pager > /tmp/log && editor-one /tmp/log"; the $EDITOR variable was already expanded to "editor-one" on line 2 and assigned to the alias "log"

In this example, the value of alias "log" is the literal string "journalctl --this-boot --no-pager > /tmp/log && editor-one /tmp/log", with "$EDITOR" replaced by the value of that variable at the time the alias was assigned.

Does that help?

Offline

#10 2013-01-26 14:59:43

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,445
Website

Re: differences between ' and "

The variable is evaluated in both cases - but with the double quotes it is evaluated to be NOTHING as it is not set at the time it is expanded.  With single quotes it is expanded on the command line when it is used.

Last edited by Trilby (2013-01-26 15:00:27)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#11 2013-01-26 15:12:08

nTia89
Banned
From: varese, italy
Registered: 2008-12-22
Posts: 1,230

Re: differences between ' and "

Trilby wrote:

The variable is evaluated in both cases - but with the double quotes it is evaluated to be NOTHING as it is not set at the time it is expanded.  With single quotes it is expanded on the command line when it is used.

yes, this was my concept of static/dynamic.....

thank you to everybody who contribute, specially Trilby


+pc: custom | AMD Opteron 175 | nForce4 Ultra | 2GB ram DDR400 | nVidia 9800GT 1GB | ArchLinux x86_64 w/ openbox
+laptop: Apple | MacBook (2,1) | 2GB ram | Mac OS X 10.4 -> DIED
+ultrabook: Dell | XPS 13 (9343) | 8GB ram | 256GB ssd | FullHD display | Windows 8.1 64bit ArchLinux x86_64 w/ Gnome

Offline

#12 2013-01-26 15:23:14

ackt1c
Banned
From: Visalia, California
Registered: 2012-10-10
Posts: 241

Re: differences between ' and "

Well

Last edited by ackt1c (2022-11-05 14:00:20)

Offline

Board footer

Powered by FluxBB