Tag Info

Hot answers tagged

65

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


23

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


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


19

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


15

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' ...


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


10

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() { ...


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" ] ...


9

Add zsh to /etc/shells: command -v zsh | sudo tee -a /etc/shells You can now use chsh to set zsh as shell: sudo chsh -s "$(command -v zsh)" "${USER}" See this documentation: Changing your login shell


9

Zsh stores input lines (possibly with time information) in the file indicated by the variable HISTFILE. This should be an absolute file name (otherwise it will be interpreted relative to whatever directory is current at the time). Zsh has no built-in default value for HISTFILE. The zsh distribution comes with a setup wizard for new users which has the value ...


8

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.


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

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


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


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 probably closest syntax to that is: p_out='/some/path' arg_1='5' my_script


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

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

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


6

You misread that comment. In Bourne-style shells such as bash and zsh, the command set is a builtin which does two things: it sets shell options, e.g. set -x turns on the xtrace (print debugging traces) option; when called with non-option arguments, it sets the positional parameters ($1, $2, …, collectively accessed as "$@"). As an exception, if the ...


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

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


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


6

No need for ls here. It's the shell that lists the directory content by expanding the *.sql glob. On a GNU or FreeBSD system: printf '%s\0' *.sql | sort -nz | xargs -r0 cat -- (using \0 instead of \n together with -z/-0 makes sure it also works with file names containing newline characters). Or if you have zsh: cat ./*.sql(.n) (The n glob qualifier ...



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