Tag Info

Hot answers tagged

7

Python comes out fairly concise, and the code Does What It Says On The Tin: python -c "import sys; print min(sys.stdin, key=len)," The final comma is obscure, I admit. It prevents the print statement adding an additional linebreak.


4

With ksh93, if you use ksh's own syntax for funtion definition (function f {...;}), then options and traps have a local scope. So there, you can do: function f { trap 'return 99' ERR set -e echo foo false echo never output } f echo "f exited with status $?" Which gives: foo f exited with status 99


4

Sub shell might be useful. func() {(set -e echo a ehco b echo c )} func func func This script produces: a script.sh: line 3: ehco: command not found a script.sh: line 3: ehco: command not found a script.sh: line 3: ehco: command not found Alternatively you might be interested in this try/catch implemetation in bash.


4

For zsh, which is shorthand for whence -c, and supports other whence options. In particular: -p Do a path search for name even if it is an alias, reserved word, shell function or builtin. So: $ which git git: aliased to noglob git $ which -p git /usr/bin/git


4

zsh prompt expansion lets you do that prompt without having to do any calculation: PS1='%$COLUMNS>╡>%F{cyan}╔╡%F{red}[%n]%F{cyan}:%F{yellow}[%m]%F{cyan}➾%F{green}[%~]%F{default}$PS1_GIT%F{cyan}${(l:COLUMNS::═:):-}%<< ╚═╡%F{default}' Explained: %$COLUMNS>╡>: right-truncate to $COLUMNS (a variable set dynamically by zsh to the width of ...


3

You're using the wrong variable name. The $HOSTNAME environment variable is often set by the host system's init routines, but not always. In the context of a ZSH profile you should use the environment variable $HOST which is explicitly set by the shell. From man zshall: PARAMETERS SET BY THE SHELL     […] ...


3

You can set the ERR trap, which is executed whenever a command returns a nonzero status. In bash, it's a bit fiddly because traps are not local to functions. By default, traps are not inherited by subshells or subfunctions; call set -E to change this. Here's some demo code. #!/bin/bash subroutine () { false echo "Subroutine continued after status $?" ...


3

Borrowing some of @mikeserv's ideas: < testfile sed 'h;s/./:/g;s/.*/expr length "&"/e;G;s/\n/\t/' | \ sort -n | \ sed -n '1s/^[0-9]+*\t//p' The first sed does the following: h saves the original line to the hold buffer Replace every character in the line with : - this is to remove any danger of code injection Replace the whole line with expr ...


3

emulate -R ksh -c 'source ~/.bashrc' This tells zsh to emulate ksh while it's loading .bashrc, so it'll by and large apply ksh parsing rules. Zsh doesn't have a bash emulation mode, ksh is as close as it gets. Furthermore when a function defined in .bashrc is executed, ksh emulation mode will be enabled during the evaluation of the function as well. ...


2

I think you want to do: export PROMPT='[%n]%t ' setenv doesn't seem to work with Zsh version 5.0.8, which is what I tried it with. Try man zshmisc and look for the section "SIMPLE PROMPT ESCAPES" to see all of the escape-sequences available. Also, take a look at zsh's RPROMPT environment variable. It can be useful and fun.


2

That's the effect of MULTIOS. echo foo >&2 | grep foo will write foo to stderr and also pipe foo to grep. Because stderr defaults to terminal, you will see two foo lines, one from echo, one from the grep result. { echo foo >&2 | grep foo } >/dev/null Here, you saw one line because stdout was redirected to /dev/null, you only saw the ...


2

Does your setopt output mention noclobber? If so, that's it, just setopt clobber


2

Another perl solution: store the lines in a hash-of-arrays, the hash key being the line length. Then, print out the lines with the minimum key. perl -MList::Util=min -ne ' push @{$lines{ length() }}, $_; } END { print @{$lines{ min keys %lines }}; ' sample 4for


2

To get just the first shortest line: f=file; sed -n "/^$(sed 's/./1/g' $f | sort -ns | sed 's/././g;q')$/{p;q}" $f To get all the shortest lints, just change {p;q} to p Another method (somewhat unusual) is to have sort do the actual sort by length. It is relatively slow even with short lines, and becomes dramatically slower as the line length ...


2

I found the answer after I posted an issue on Oh-My-Zsh's repository. Color in tree rely on LS_COLORS, which is not set by Zsh by default; but my ~/.zshrc set the variable after I hit "use default setting" option, with a single line eval "$(dircolors -b)" which looks insignificant. After installing Oh-My-Zsh, the setting is moved to ...


2

What about using sort? awk '{ print length($0) "\t" $0 }' input.txt | sort -n | head -n 1 | cut -f2-


2

Assuming blank lines are not considered the shortest line and that blank lines might exist, the following pure AWK will work: awk ' { len = length; a[$0] = len } !len { next } !min { min = len } len < min { min = len } END { for (i in a) if (min == a[i]) print i } ' ...


2

It occurred to me that the whole thing is possible in one sed expression. It ain't pretty: $ sed '1h;s/.*/&\n&/;G;:l;s/\n[^\n]\([^\n]*\)\n[^\n]/\n\1\n/;tl;/\n\n/{s/\n.*//;x};${x;p};d' testfile 4for $ Breaking this down: 1h # save line 1 in the hold buffer (shortest line so far) s/.*/&\n&/ # duplicate the line with a ...


2

There are various issues here. First, == is not standard, the POSIX way is =. Same goes for the -o. This one will work on both bash and zsh: for f in ~/*; do if [ "$f" = '/home/sk/.' ] || [ "$f" = '/home/sk/..' ]; then true else echo "$f" fi done Note that your if is unneeded, dotfiles are ignored by default in both bash and zsh. You can ...


2

Try: if [ "$f" = '/home/sk/.' ] || [ "$f" = '/home/sk/..' ]


2

In zsh, Command Substitution result was performed word splitting if was not enclosed in double quotes. So if your command substitution result contain any whitespace, tab, or newline, the export command will be broken into parts: $ export a=$(echo 1 -2) export: not valid in this context: -2 You need to double quote command substitution to make it work, or ...


2

Try /usr/bin/which git it should give you some file path, probably /usr/bin/git


1

Ok, I've found the solution: deleting all zcompdump files solved the problem: rm ~/.zcompdump*


1

You should just set HOSTNAME=$(hostname) in your ~/.zshrc Or as Caleb pointed out there is a variable HOST set, so to keep your prompt portable you could also do: HOSTNAME=$HOST


1

I think I got it, for POSIX compliance I need double quotes here. The following fixed it. export CONDA_ENV_PATH="$(get_dirname "$_THIS_DIR")" The following excellent article may be helpful: When is double-quoting necessary?


1

Not a direct answer to your question, but the conditions are unnecessary, since in the POSIX spec * will not expand to . or .. Also, you mentioned wanting to print all folders so then you would need to specify a trailing / in the pattern So something like this should suffice to print all non-hidden directories.. for d in ~/*/ do [ -d "$f" ] ...


1

A classical way to extract a specific line from an output is by combining head and tail commands: LINE=3 ls | head -n $LINE | tail -n 1 ls command generates the initial output, head -n $LINE keeps only the first $LINE lines from this original output, tail -n 1 keeps only the last line from the head command output. At the end only the line at the given ...



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