0

How do I set a variable in Bash Zsh with a single dash (-)?

None of these work:

x=-
x=\-
x='-' 

EDIT: this seems to work fine in Bash, I was testing this with echo in Zsh which doesn't work.

AdminBee
  • 21,637
  • 21
  • 47
  • 71
Subbeh
  • 430
  • 2
  • 9
  • 3
    All of those assignments work. Perhaps your problem is elsewhere. How are you using that variable? – ilkkachu Sep 22 '21 at 22:17
  • I'm an idiot, I was testing this with echo in zsh.. thanks – Subbeh Sep 22 '21 at 22:21
  • Answer your own question explaining what was going on, for later reference. – vonbrand Sep 22 '21 at 22:22
  • 5
    @vonbrand, [Why is printf better than echo?](https://unix.stackexchange.com/q/65803/170373) comes to mind. Subbeh, you're not an idiot, it's just that `echo` sucks. – ilkkachu Sep 22 '21 at 22:25

1 Answers1

0

The problem was with echo. The variable is assigned correctly but had to be used with echo - $x instead.

Subbeh
  • 430
  • 2
  • 9