Why doesn't var=- work?
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
In writing the question:
How to touch
and cat
file named -
I was trying to generalise to a case where a file named -
was stored in a variable.
In zsh
, I tried setting a variable to contain a single hyphen:
% var=-
% echo $var
% var='-'
% echo $var
% var=-
% echo $var
%
Why don't these work?
Why is zsh
different to bash
in this regard?
How do I set $var
to -
?
zsh
add a comment |Â
up vote
0
down vote
favorite
In writing the question:
How to touch
and cat
file named -
I was trying to generalise to a case where a file named -
was stored in a variable.
In zsh
, I tried setting a variable to contain a single hyphen:
% var=-
% echo $var
% var='-'
% echo $var
% var=-
% echo $var
%
Why don't these work?
Why is zsh
different to bash
in this regard?
How do I set $var
to -
?
zsh
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In writing the question:
How to touch
and cat
file named -
I was trying to generalise to a case where a file named -
was stored in a variable.
In zsh
, I tried setting a variable to contain a single hyphen:
% var=-
% echo $var
% var='-'
% echo $var
% var=-
% echo $var
%
Why don't these work?
Why is zsh
different to bash
in this regard?
How do I set $var
to -
?
zsh
In writing the question:
How to touch
and cat
file named -
I was trying to generalise to a case where a file named -
was stored in a variable.
In zsh
, I tried setting a variable to contain a single hyphen:
% var=-
% echo $var
% var='-'
% echo $var
% var=-
% echo $var
%
Why don't these work?
Why is zsh
different to bash
in this regard?
How do I set $var
to -
?
zsh
zsh
asked Sep 8 at 13:23
Tom Hale
5,94622776
5,94622776
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
7
down vote
accepted
Again, as often, it's not the value actually in the variable, but how the variable is. The echo
in this case. Zsh's echo
takes the single dash as an end of options indicator, so it's removed. Online manual:
Note that for standards compliance a double dash does not terminate option processing; instead, it is printed directly. However, a single dash does terminate option processing, so the first dash, possibly following options, is not printed, but everything following it is printed as an argument. The single dash behaviour is different from other shells. For a more portable way of printing text, see printf, and for a more controllable way of printing text within zsh, see print.
So we have:
zsh% echo -
zsh% echo - -n
-n
zsh% var=-
zsh% printf "%sn" "$var"
-
See also:
- Why is printf better than echo?
So this needs to be seen as a bug inzsh
.
â schily
Sep 8 at 15:18
1
@schily, sure, go ahead and report is as such.
â ilkkachu
Sep 8 at 15:24
See also: zsh.org/mla/workers/2018/msg00202.html
â Stéphane Chazelas
Sep 9 at 18:40
@schily : I wouldn't see it as a bug. The behaviour is exactly as documented in the zsh man page. Note that if you don't like this behaviour of theecho
builtin in Zsh, you can always use, i.e.,command echo $var
instead ofecho $var
and your dash will be printed.
â user1934428
Sep 10 at 8:46
@user1934428, using the system'secho
will not necessarily help. For instance, on GNU systems, you'll still have problems for values of$var
like-n
,--version
,-Ene
... Inzsh
,echo -E - $var
,print -r -- $var
andprintf '%sn' "$var"
should be equivalent but only the latter one is portable to other Bourne-like shells which is why zsh and POSIX recommend to useprintf
here. Only zsh and yashecho
s are able to output arbitrary data.
â Stéphane Chazelas
Sep 10 at 10:09
 |Â
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
7
down vote
accepted
Again, as often, it's not the value actually in the variable, but how the variable is. The echo
in this case. Zsh's echo
takes the single dash as an end of options indicator, so it's removed. Online manual:
Note that for standards compliance a double dash does not terminate option processing; instead, it is printed directly. However, a single dash does terminate option processing, so the first dash, possibly following options, is not printed, but everything following it is printed as an argument. The single dash behaviour is different from other shells. For a more portable way of printing text, see printf, and for a more controllable way of printing text within zsh, see print.
So we have:
zsh% echo -
zsh% echo - -n
-n
zsh% var=-
zsh% printf "%sn" "$var"
-
See also:
- Why is printf better than echo?
So this needs to be seen as a bug inzsh
.
â schily
Sep 8 at 15:18
1
@schily, sure, go ahead and report is as such.
â ilkkachu
Sep 8 at 15:24
See also: zsh.org/mla/workers/2018/msg00202.html
â Stéphane Chazelas
Sep 9 at 18:40
@schily : I wouldn't see it as a bug. The behaviour is exactly as documented in the zsh man page. Note that if you don't like this behaviour of theecho
builtin in Zsh, you can always use, i.e.,command echo $var
instead ofecho $var
and your dash will be printed.
â user1934428
Sep 10 at 8:46
@user1934428, using the system'secho
will not necessarily help. For instance, on GNU systems, you'll still have problems for values of$var
like-n
,--version
,-Ene
... Inzsh
,echo -E - $var
,print -r -- $var
andprintf '%sn' "$var"
should be equivalent but only the latter one is portable to other Bourne-like shells which is why zsh and POSIX recommend to useprintf
here. Only zsh and yashecho
s are able to output arbitrary data.
â Stéphane Chazelas
Sep 10 at 10:09
 |Â
show 2 more comments
up vote
7
down vote
accepted
Again, as often, it's not the value actually in the variable, but how the variable is. The echo
in this case. Zsh's echo
takes the single dash as an end of options indicator, so it's removed. Online manual:
Note that for standards compliance a double dash does not terminate option processing; instead, it is printed directly. However, a single dash does terminate option processing, so the first dash, possibly following options, is not printed, but everything following it is printed as an argument. The single dash behaviour is different from other shells. For a more portable way of printing text, see printf, and for a more controllable way of printing text within zsh, see print.
So we have:
zsh% echo -
zsh% echo - -n
-n
zsh% var=-
zsh% printf "%sn" "$var"
-
See also:
- Why is printf better than echo?
So this needs to be seen as a bug inzsh
.
â schily
Sep 8 at 15:18
1
@schily, sure, go ahead and report is as such.
â ilkkachu
Sep 8 at 15:24
See also: zsh.org/mla/workers/2018/msg00202.html
â Stéphane Chazelas
Sep 9 at 18:40
@schily : I wouldn't see it as a bug. The behaviour is exactly as documented in the zsh man page. Note that if you don't like this behaviour of theecho
builtin in Zsh, you can always use, i.e.,command echo $var
instead ofecho $var
and your dash will be printed.
â user1934428
Sep 10 at 8:46
@user1934428, using the system'secho
will not necessarily help. For instance, on GNU systems, you'll still have problems for values of$var
like-n
,--version
,-Ene
... Inzsh
,echo -E - $var
,print -r -- $var
andprintf '%sn' "$var"
should be equivalent but only the latter one is portable to other Bourne-like shells which is why zsh and POSIX recommend to useprintf
here. Only zsh and yashecho
s are able to output arbitrary data.
â Stéphane Chazelas
Sep 10 at 10:09
 |Â
show 2 more comments
up vote
7
down vote
accepted
up vote
7
down vote
accepted
Again, as often, it's not the value actually in the variable, but how the variable is. The echo
in this case. Zsh's echo
takes the single dash as an end of options indicator, so it's removed. Online manual:
Note that for standards compliance a double dash does not terminate option processing; instead, it is printed directly. However, a single dash does terminate option processing, so the first dash, possibly following options, is not printed, but everything following it is printed as an argument. The single dash behaviour is different from other shells. For a more portable way of printing text, see printf, and for a more controllable way of printing text within zsh, see print.
So we have:
zsh% echo -
zsh% echo - -n
-n
zsh% var=-
zsh% printf "%sn" "$var"
-
See also:
- Why is printf better than echo?
Again, as often, it's not the value actually in the variable, but how the variable is. The echo
in this case. Zsh's echo
takes the single dash as an end of options indicator, so it's removed. Online manual:
Note that for standards compliance a double dash does not terminate option processing; instead, it is printed directly. However, a single dash does terminate option processing, so the first dash, possibly following options, is not printed, but everything following it is printed as an argument. The single dash behaviour is different from other shells. For a more portable way of printing text, see printf, and for a more controllable way of printing text within zsh, see print.
So we have:
zsh% echo -
zsh% echo - -n
-n
zsh% var=-
zsh% printf "%sn" "$var"
-
See also:
- Why is printf better than echo?
edited Sep 8 at 13:46
answered Sep 8 at 13:41
ilkkachu
52.1k679144
52.1k679144
So this needs to be seen as a bug inzsh
.
â schily
Sep 8 at 15:18
1
@schily, sure, go ahead and report is as such.
â ilkkachu
Sep 8 at 15:24
See also: zsh.org/mla/workers/2018/msg00202.html
â Stéphane Chazelas
Sep 9 at 18:40
@schily : I wouldn't see it as a bug. The behaviour is exactly as documented in the zsh man page. Note that if you don't like this behaviour of theecho
builtin in Zsh, you can always use, i.e.,command echo $var
instead ofecho $var
and your dash will be printed.
â user1934428
Sep 10 at 8:46
@user1934428, using the system'secho
will not necessarily help. For instance, on GNU systems, you'll still have problems for values of$var
like-n
,--version
,-Ene
... Inzsh
,echo -E - $var
,print -r -- $var
andprintf '%sn' "$var"
should be equivalent but only the latter one is portable to other Bourne-like shells which is why zsh and POSIX recommend to useprintf
here. Only zsh and yashecho
s are able to output arbitrary data.
â Stéphane Chazelas
Sep 10 at 10:09
 |Â
show 2 more comments
So this needs to be seen as a bug inzsh
.
â schily
Sep 8 at 15:18
1
@schily, sure, go ahead and report is as such.
â ilkkachu
Sep 8 at 15:24
See also: zsh.org/mla/workers/2018/msg00202.html
â Stéphane Chazelas
Sep 9 at 18:40
@schily : I wouldn't see it as a bug. The behaviour is exactly as documented in the zsh man page. Note that if you don't like this behaviour of theecho
builtin in Zsh, you can always use, i.e.,command echo $var
instead ofecho $var
and your dash will be printed.
â user1934428
Sep 10 at 8:46
@user1934428, using the system'secho
will not necessarily help. For instance, on GNU systems, you'll still have problems for values of$var
like-n
,--version
,-Ene
... Inzsh
,echo -E - $var
,print -r -- $var
andprintf '%sn' "$var"
should be equivalent but only the latter one is portable to other Bourne-like shells which is why zsh and POSIX recommend to useprintf
here. Only zsh and yashecho
s are able to output arbitrary data.
â Stéphane Chazelas
Sep 10 at 10:09
So this needs to be seen as a bug in
zsh
.â schily
Sep 8 at 15:18
So this needs to be seen as a bug in
zsh
.â schily
Sep 8 at 15:18
1
1
@schily, sure, go ahead and report is as such.
â ilkkachu
Sep 8 at 15:24
@schily, sure, go ahead and report is as such.
â ilkkachu
Sep 8 at 15:24
See also: zsh.org/mla/workers/2018/msg00202.html
â Stéphane Chazelas
Sep 9 at 18:40
See also: zsh.org/mla/workers/2018/msg00202.html
â Stéphane Chazelas
Sep 9 at 18:40
@schily : I wouldn't see it as a bug. The behaviour is exactly as documented in the zsh man page. Note that if you don't like this behaviour of the
echo
builtin in Zsh, you can always use, i.e., command echo $var
instead of echo $var
and your dash will be printed.â user1934428
Sep 10 at 8:46
@schily : I wouldn't see it as a bug. The behaviour is exactly as documented in the zsh man page. Note that if you don't like this behaviour of the
echo
builtin in Zsh, you can always use, i.e., command echo $var
instead of echo $var
and your dash will be printed.â user1934428
Sep 10 at 8:46
@user1934428, using the system's
echo
will not necessarily help. For instance, on GNU systems, you'll still have problems for values of $var
like -n
, --version
, -Ene
... In zsh
, echo -E - $var
, print -r -- $var
and printf '%sn' "$var"
should be equivalent but only the latter one is portable to other Bourne-like shells which is why zsh and POSIX recommend to use printf
here. Only zsh and yash echo
s are able to output arbitrary data.â Stéphane Chazelas
Sep 10 at 10:09
@user1934428, using the system's
echo
will not necessarily help. For instance, on GNU systems, you'll still have problems for values of $var
like -n
, --version
, -Ene
... In zsh
, echo -E - $var
, print -r -- $var
and printf '%sn' "$var"
should be equivalent but only the latter one is portable to other Bourne-like shells which is why zsh and POSIX recommend to use printf
here. Only zsh and yash echo
s are able to output arbitrary data.â Stéphane Chazelas
Sep 10 at 10:09
 |Â
show 2 more comments
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f467712%2fwhy-doesnt-var-work%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password