Tag Info

Hot answers tagged

69

Citing the Open Group Specification on Shell Command Languages, specifically this section "2.6.3 Command Substitution" this statement regarding backtick's deprecation is actually false. Both forms of the command substitution, backticks (`..cmd..`) or dollar parens ($(..cmd..)) are still supported in so far as the specification goes. excerpt Command ...


24

You didn't specify which shell you are using, so let this be excuse to advertise zsh. Yes, we do have more history for cd, namely cd -2, cd -4 etc. Very convenient is cd -TAB, especially with completion system and colors enabled: This is what I have in .zshrc: setopt AUTO_PUSHD setopt PUSHD_MINUS setopt CDABLE_VARS zstyle ':completion:*:directory-stack' ...


23

You can use brace expansions: convert -trim -density 400 this_is_a_very_long_filename_of_my_pdf_file.{pdf,png}


23

The command you are looking for is pushd and popd. You could view a practical working example of pushd and popd from here. mkdir /tmp/dir1 mkdir /tmp/dir2 mkdir /tmp/dir3 mkdir /tmp/dir4 cd /tmp/dir1 pushd . cd /tmp/dir2 pushd . cd /tmp/dir3 pushd . cd /tmp/dir4 pushd . dirs /tmp/dir4 /tmp/dir4 /tmp/dir3 /tmp/dir2 /tmp/dir1


22

With (t)csh, bash or zsh history expansion you could write: vagrant up && !#:0 ssh But, seriously, you wouldn't


20

You cannot solve this through shebang directly, since shebang is purely static. What you could do is having some Ā»least common multiplierĀ« (from a shell perspective) in the shebang and re-execute your script with the right shell, if this LCM isn't zsh. In other words: Have your script executed by a shell found on all systems, test for a zsh-only feature ...


14

Use printf builtin: $ printf %s\\n localhost:8080/reports/{promos,promo-updates,scandown}/{130,139,142}{,-unburdened,-burdened}{,.pdf,.xls,.xlsx,.csv,.preload} localhost:8080/reports/promos/130 localhost:8080/reports/promos/130.pdf localhost:8080/reports/promos/130.xls localhost:8080/reports/promos/130.xlsx localhost:8080/reports/promos/130.csv ...


13

You could do: eval vagrant\ {'up &&',ssh} but that would not make it easier. You could always make a function doall() { cmd=$1; shift for arg do "$cmd" "$arg" || return done } And then: doall vagrant up ssh


12

If you don't mind being limited to single-letter argument names i.e. my_script -p '/some/path' -a5, then in bash you could use the built-in getopts, e.g. #!/bin/bash while getopts ":a:p:" opt; do case $opt in a) p_out="$OPTARG" ;; p) arg_1="$OPTARG" ;; \?) echo "Invalid option -$OPTARG" >&2 ;; esac done printf "Argument ...


11

With zsh, it's just: mypath=$0:A Now for other shells, though realpath() and readlink() are standard functions (the latter being a system call), realpath and readlink are not standard command, though some systems have one or the other or both with various behaviour and feature set. As often, for portability, you may want to resort to perl: abs_path() { ...


11

One difference between the two is that aliases are only a shell feature. Environment variables are inherited by all subprocesses (unless deliberately cleared). The environment variable would be more likely to work even if less is launched indirectly, such as via another shell (e.g. tcsh), man, vim, psql, etc.


10

Create .bash_profile in your home directory and add these lines: export SHELL=/bin/zsh exec /bin/zsh -l Update: .profile may work as a general solution when default shell is not bash. I'm not sure if .profile may be called by Zsh as well that it could go redundant but we can do it safely with a simple check: export SHELL=/bin/zsh [ -z "$ZSH_VERSION" ] ...


10

To answer your question regarding "more history". No the cd - feature in Bash only supports a single directory that you can "flip" back to. As @Ramesh states in his answer. If you want a longer history of directories you can use pushd and popd to save a directory or return to a previous one. You can also see the list of what's currently in the stack with ...


9

Yes, this happens because it is a "partial line". And by default zsh goes to the next line to avoid covering it with the prompt. When a partial line is preserved, by default you will see an inverse+bold character at the end of the partial line: a "%" for a normal user or a "#" for root. If set, the shell parameter PROMPT_EOL_MARK can be used to ...


8

The probably closest syntax to that is: p_out='/some/path' arg_1='5' my_script


8

This is happening because cut is outputting NULL characters in the output. You can't pass a program arguments which contain a null character (see this). In bash this works because bash can't handle NULL characters in strings, and it strips them out. Zsh is a bit more powerful, and it can handle NULL characters. However when it comes time to pass the string ...


8

The thousands separator is a GNU extension that zsh doesn't support, and it has its own printf builtin that you end up using instead. As mentioned in the linked post, you can get the locale-dependant thousands separator with: zmodload zsh/langinfo echo $langinfo[THOUSEP] If you need to use zsh specifically and exclusively, you can use that with sed. ...


8

Different, documented behavior in zshmisc A list is a sequence of zero or more sublists, in which each sublist is terminated by ;, &, &|, &!, or a newline. This terminator may optionally be omitted from the last sublist in the list when the list appears as a complex command inside (...) or {...}. When a sublist is terminated ...


8

Because there's no command called emacs -nw. There's a command called emacs to which you can pass a -nw option. To store commands, you generally use functions: foo() emacs -nw "$@" foo ... To store several arguments, you generally use arrays: foo=(emacs -nw) $foo ... To store a string containing several words separated by spaces and have it split on ...


7

For years I've used something similar to deal with the various locations of Bash on systems that I needed my scripts to run. Bash/Zsh/etc. #!/bin/sh # Determines which OS and then reruns this script with approp. shell interp. LIN_BASH="/bin/sh"; SOL_BASH="/packages/utilities/bin/sun5/bash"; OS_TYPE=`uname -s`; if [ $OS_TYPE = "SunOS" ]; then ...


7

The TERM environment variable should be set by the application that is acting as your terminal. This is the whole point of the thing: letting programs running inside them know what terminal is being used and hence what sort of features it supports. Zsh is not a terminal. It is a shell. It might care what your TERM is set to if it wants to do special things, ...


7

You could store the brace expansion in an array, then output it in the manner of your choosing: urls=( localhost:8080/reports/{promos,promo-updates,scandown}/{130,139,142}{,-unburdened,-burdened}{,.pdf,.xls,.xlsx,.csv,.preload} ) Then printf "%s\n" "${urls[@]}" or (IFS=$'\n'; echo "${urls[*]}") The echo example looks weird because: it's run in a ...


7

The best way, to look at zsh documentation is using info. If you run info zsh, you can use the index (think of a book's index) to locate the section that describes the zmodload command. Press i, then you can enter zmo and press Tab. You'll get straight to the zmodload builtin description which will tell you all about it. In short, zmodload -F loads the ...


7

With zsh, use glob qualifiers: mv home*(.) dst moves only regular files. While mv home*(^/) dst moves files of any type except directories. mv home*(^-/) dst would also exclude symlinks to directories.


7

The purpose of ${1+"$@"} for portability. POSIX defined $@ would expand to nothing if there're no positional arguments. But original Bourne shell (or /bin/sh in Solaris 10 and before) would expand it to "". Using ${1+"$@"} is a work around for this, since when "$@" only expanded if $1 was set. Unfortunately, this construct doesn't work in zsh 3.x and pre ...


6

Alias substitution is only performed when reading lines from interactive sources. So the second alias is not affected by the first, hence the literal replacement. Perhaps something similar: PACMAN=pacman if [ -f /usr/bin/pacmatic ]; then PACMAN=pacmatic fi # Colorized Pacman output alias pacman="${PACMAN} --color auto" This'll set 'pacman' to the ...


6

Thats three questions in one ;-) AUTO_CD Option and howto find it First of all the option you are looking for is AUTO_CD. You can easily find it by looking up man zshoptions. Use your pagers search function, usually you press / and enter the keyword. With n you jump to the next occurrence. This will bring up the following: [..] Changing Directories ...


6

Warning: With any of these solutions, you need to be aware that you are trusting the integrity of the data files to be safe as they will get executed as shell code in your script. Securing them is paramount to your script's security! Simple inline implementation for serializing one or more variables Yes, in both bash and zsh you can serialize the contents ...


6

This is tagged zsh, so I suggest the zsh builtin print : print -l localhost:8080/reports/{promos,promo-updates,scandown}/{130,139,142}{,-unburdened,-burdened}{,.pdf,.xls,.xlsx,.csv,.preload} -l prints arguments on separate lines.


6

You can install and use my dirhistory utility for bash. Basically, it's a daemon that collects directory changes from all your shells, and a Cdk program that displays the history and lets you pick any directory to switch to (so you're not limited to a stack).



Only top voted, non community-wiki answers of a minimum length are eligible