zsh variable behave like bash variable

setopt shwordsplit
for exemple : var="echo hello"; $var this will display "hello" with bash with zsh, by default, this will make an error : "command not found : echo hello" hwordsplit option permit zsh to act like bash on this point

0
By: Gentux
2010-03-08 20:52:55

These Might Interest You

  • I wrote this script to speed up Nginx configs. This (long) one liner can be run via BASH. You will see that we set a variable in bash called 'foo' and the streamline editor (sed) finds 'bar' in 'foo.conf' next it writes that output to a temp file (foo.temp) and removes the first 5 lines (that aren't needed in this case) & lastly it moves (overwrites) foo.temp to foo.conf Show Sample Output


    0
    variable="foo" && sed 's/bar/'$variable'/g' $variable.conf >> $variable.temp && sed '1,5d' $variable.temp && mv $variable.temp $variable.conf
    jdorfman · 2010-07-09 22:12:51 0
  • This doesn't work in bash, but in zsh you can typeset -T to bind a scalar variable to an array. $PATH and $path behave this way by default.


    -5
    print -l $path
    Mikachu · 2009-08-27 16:33:04 0
  • Set a bookmark as normal shell variable p=/cumbersome/path/to/project To go there to p This saves one "$" and is faster to type ;-) The variable is still useful as such: vim $p/<TAB> will expand the variable (at least in bash) and show a list of files to edit. If setting the bookmarks is too much typing you could add another function bm() { eval $1=$(pwd); } then bookmark the current directory with bm p


    4
    to() { eval dir=\$$1; cd "$dir"; }
    hfs · 2010-10-15 13:40:35 3
  • By default bash expands an unbound variable to an empty string. This can be dangerous, if a critical variable name (a path prefix for example) has a typo. The -u option causes bash to treat this as an error, and the -e option causes it to exit in case of an error. These two together will make your scripts a lot safer against typos. The default behaviour can be explicitly requested using the ${NAME:-} syntax. A (less explicit) variation: #!/bin/bash -eu Show Sample Output


    8
    set -eu
    wipu · 2010-04-07 11:54:40 2

What Others Think

This will also work for temporary use on a single var (no matter what shwordsplit is set as): var="echo hello"; ${=var}
Vilemirth · 235 weeks and 5 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: