Why doesn't var=- work?

The name of the pictureThe name of the pictureThe name of the pictureClash 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 -?










share|improve this question

























    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 -?










    share|improve this question























      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 -?










      share|improve this question













      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 8 at 13:23









      Tom Hale

      5,94622776




      5,94622776




















          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?





          share|improve this answer






















          • So this needs to be seen as a bug in zsh.
            – 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 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 echos are able to output arbitrary data.
            – Stéphane Chazelas
            Sep 10 at 10:09











          Your Answer







          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "106"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          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






























          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?





          share|improve this answer






















          • So this needs to be seen as a bug in zsh.
            – 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 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 echos are able to output arbitrary data.
            – Stéphane Chazelas
            Sep 10 at 10:09















          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?





          share|improve this answer






















          • So this needs to be seen as a bug in zsh.
            – 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 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 echos are able to output arbitrary data.
            – Stéphane Chazelas
            Sep 10 at 10:09













          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?





          share|improve this answer














          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?






          share|improve this answer














          share|improve this answer



          share|improve this answer








          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 in zsh.
            – 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 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 echos 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






          • 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 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 echos 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 echos 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 echos are able to output arbitrary data.
          – Stéphane Chazelas
          Sep 10 at 10:09


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          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













































































          Popular posts from this blog

          How to check contact read email or not when send email to Individual?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay