Tag Info

Hot answers tagged

21

There is no significance to exiting with code 99, other than there is perhaps in the context of a specific program. Either way, exit exits the shell with a certain exit code, in this case, 99. You can find more information in help exit: exit: exit [n] Exit the shell. Exits the shell with a status of N. If N is omitted, the exit status is that ...


19

The output of the clear command is console escape codes. The exact codes required depend on the exact terminal you are using, however most use ANSI control sequences. Here is a good link explaining the various codes - http://www.termsys.demon.co.uk/vtansi.htm. The relevant snippets are: Cursor Home <ESC>[{ROW};{COLUMN}H Sets the cursor ...


15

It works by issuing certain ANSI escape sequences. Specifically, these two: Esc[Line;ColumnH          Cursor Position: Esc[Line;Columnf            Moves the cursor to the specified position (coordinates). If you do not ...


12

The following will convert each environment variable into an export statement, properly quoted for reading into a shell (because LS_COLORS, for example, is likely to have semicolons in it), then sources it. [The printf in /usr/bin, unfortunately, generally doesn't support %q, so we need to call the one built into bash.] . <(xargs -0 bash -c 'printf ...


11

Without quotes the string is subject to word splitting and globbing. See also BashPitfalls #14. Compare $ echo $(printf 'foo\nbar\nquux\n*') foo bar quux ssh-13yzvBMwVYgn ssh-3JIxkphQ07Ei ssh-6YC5dbnk1wOc with $ echo "$(printf 'foo\nbar\nquux\n*')" foo bar quux * When word splitting occurs the first character of IFS acts as a separator (which, per ...


11

In addition to @Chris Down, there is some return code that reserved for the shell, they have special meaning: RETVAL Meaning 1 General errors 2 Misusage 127 Command not found You can refer to this for more details.


9

The output sent by clear(1) depends on your terminal type, defined by $TERM in the shell environment. It does the same thing as the command "tput clear", which is looking up the escape code for the current terminal type and sending that string to standard output. The terminal receiving the escape code from clear/tput interprets it and executes the command ...


8

In this answer, I assume a system where /proc/$pid/environ returns the environment of the process with the specified PID, with null bytes between variable definitions. (So Linux, Cygwin or Solaris (?)). Zsh export "${(@ps:\000:)$(</proc/$pid/environ)}" (Pretty simple as zsh goes: an input redirection with no command <FILE is equivalent to cat FILE. ...


8

You have used system function, so it will use another shell to run the command which which. From man system: DESCRIPTION system() executes a command specified in command by calling /bin/sh -c command, and returns after the command has been completed. During exe‐ cution of the command, SIGCHLD will be blocked, and SIGINT and SIGQUIT ...


7

In bash you can do the following. This will work for all possible contents of the variables and avoids eval: while IFS= read -rd '' var; do declare +x "$var"; done </proc/$PID/environ This will declare the read variables as shell variables in the running shell. To export the variables into the running shell environment instead: while IFS= read -rd '' ...


7

This is simply done with an alias; alias ls="ls -1" You can put this in your .bashrc file, although it probably already contains the following alias to give colourised output: alias ls="ls --color=auto" In which case you would just add to it giving: alias ls="ls --color=auto -1"


6

The main difference is that the quoted version is not subject to field splitting by the shell. With double quotes the outcome of the command expansion would be fed as one parameter to the source command. Without quotes it would be broken up into multiple parameters, depending on the value of IFS which contains space, TAB and newline by default. If the ...


6

use here-docs to get around all of the nasty subshell quoting: ssh you@host <<-\SSH awk -f 3<<\AWK /dev/fd/3 awk script as many lines as you like "$vars and quotes" are only evaluated by awk #END AWK "$vars and quotes" are only evaluated by remote shell echo 'single quotes and all' rest of ssh ...


6

As Josh Jolly said in his answer, you should never parse ls, use the approach in his answer instead. Still, here's an awk solution to remove paths from file names, just don't use it with ls: find . | awk -F'/' '{print $NF}' The -F'/' sets the field separator to / which means that the last field, $NF, will be the file name.


6

Many of the compressors take an an environment variable to accept options that cannot be passed on the command line. In your case GZIP_OPT=-9 sort --compress-program=/bin/gzip The same is true for xz with XZ_OPT and bzip2 with BZIP2


5

In addition to all nice answer above, we can do some strace to see what happen: $ strace -e trace=write echo -e "\x1b\x5b\x48\x1b\x5b\x32\x4a\c" write(1, "\33[H\33[2J", 7 $ strace -e trace=write clear write(1, "\33[H\33[2J", 7 You can see, two command provide the same ANSI escape sequences.


4

The likely most important difference would be if the directory that the script is in has a space in it. In that case, the first line, the one without the double-quotes, would fail. This would be the result of "word splitting" which bash does on unquoted strings. Suppose that the result of dirname ${BASH_SOURCE[0]} is /home/j r/bin. Consider the line ...


3

script | sed -n '${x;p};h' That should do it, I think. It will always print the second to last line. If you want only the number you: script | sed -n '${x;s/[^0-9]*\([^ ]*\).*/\1/p};h' So big H appends to sed's hold space the contents of the current pattern space, whereas little h overwrites it. So if you overwrite the hold space for every line, and on ...


3

Using source and process substitution: source <(sed -r -e 's/([^\x00]*)\x00/export \1\n/g' /proc/1/environ) Shortly: . <(sed -r -e 's/([^\x00]*)\x00/export \1\n/g' /proc/1/environ) Using eval and command substitution: eval `sed -r -e 's/([^\x00]*)\x00/export \1\n/g' /proc/1/environ` The sed call can be replaced with an awk call: awk ...


3

I am not entirely clear on how you get the output you show. I am assuming it is produced by the script you mentioned and that you can simply pipe it through something else to parse it. If so, these solutions should work: your_script | tail -n 2 | awk '/RMS/{print $4}' tail -n 2 prints the last two lines and the awk will print the 4th field of any line ...


3

How about using brace expansions? $ ls -ld /{,usr/{,bin/{,tee}}} drwxr-xr-x 23 root root 4096 Mar 7 06:57 / drwxr-xr-x 10 root root 4096 Jan 9 2013 /usr/ drwxr-xr-x 2 root root 40960 Apr 9 23:57 /usr/bin/ -rwxr-xr-x 1 root root 26176 Nov 19 2012 /usr/bin/tee


3

ls -ld `echo 'path/to/file' | sed ':0 p;s!/[^/]*$!!;t0' | sort -u` sed part: :0 label 0; p print; s!p!r! replace pattern p with replacement r; /[^/]*$ search for /, then any sequence of not-/ till the end of line; replacement is empty, so just delete the match; t0 if s!!! performs a replacement, then go to label 0. Edit by OP after comments I did the ...


3

Since you opened a quotation ' and pressed enter, the shell is wanting you to close the quote. The quote> prompt is simply a visual indication of such. This is so that you can pass multi-line arguments to programs. For example: $ echo 'hi quote> there' hi there Since you typed ls'Enterls'Enter, this is the equivalent to trying to run a command ...


3

UPDATE: I noticed your own answer had a separate function each for appending or prepending to the $PATH. I liked the idea. So I added a little argument handling. I also properly _namespaced it: _path_assign() { oFS=$IFS ; IFS=: ; add=$* ; unset P A ; A= set -- ${PATH:=$1} ; for p in $add ; do { [ -z "${p%-[AP]}" ] && { unset P A ...


3

The easiest way to link to the current directory as an absolute path, without typing the whole path string would be ln -s "$(pwd)/foo" ~/bin/foo_link The target argument for the ln -s command works relative to the symbolic link's location, not your current directory. It helps to imagine that the created symlink simply holds the text you provide for the ...


3

Some shells like zsh, bash or mksh automatically set the $COLUMNS variable to the width of the terminal, so you don't need to invoke stty here. All the implementations of ps I tried that support that non-standard (BSD-type) syntax query the terminal width by themselves. I'm surprised yours doesn't. I expect it will look at the content of the COLUMNS ...


2

I'm surprised other answers have not mentioned TERMINFO (or TERMCAP) Use the man pages Luke man clear says ... NAME clear - clear the terminal screen SYNOPSIS clear DESCRIPTION clear clears your screen if this is possible. It looks in the environ- ment for the terminal type and then in the terminfo database to figure ...


2

The following function permits to change to sibling directories (bash function) function sib() { ## sib search sibling directories ## prompt for choice (when two or more directories are found, current dir is removed from choices) ## change to directory after selection local substr=$1 local curdir=$(pwd) local choices=$(find ...


2

Screen is a bit heavy handed. A second way is to use the old school method of nohup. nohup script command 2>&1 > /dev/tty1 & The nohup command captures all hangup signals and ignores them, so the the command left after will not receive and there for not stop on closing your terminal.


2

I can't think of any expansion trick or utility to do it all in one go. So a loop is the way to go. Here's some code that works under both bash and zsh, and accommodates directories with arbitrary names. ## Usage: set_directory_chain VAR FILENAME ## Set VAR to the chain of directories leading to FILENAME ## e.g. set_directory_chain a /usr/bin/env is ...



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