Tag Info

Hot answers tagged

33

Brace expansion is very useful if you have long path names. I use it as a quick way to backup a file: cp /a/really/long/path/to/some/file.txt{,.bak} will copy /a/really/long/path/to/some/file.txt to /a/really/long/path/to/some/file.txt.bak You can also use it in a sequence. I once did so to download lots of pages from the web: wget ...


28

The single bracket [ is actually an alias for the test command, it's not syntax. One of the downsides (of many) of the single bracket is that if one or more of the operands it is trying to evaluate return an empty string, it will complain that it was expecting two operands (binary). This is why you see people do [ x$foo = x$blah ], the x guarantees that ...


24

This is used for alias protection: $ ls .bashrc a b $ alias ls alias ls='ls $LS_OPTIONS' $ \ls a b


24

Maybe not what you're asking for, but this should work to some extent to identify the interpreter currently interpreting it for a few like Thomson (osh), Bourne, Bourne-again, Korn, Z, (T)C, Policy-compliant Ordinary, Yet Another, rc, akanga, es shells, wish, tclsh, expect, perl, python, ruby, php, JavaScript (SpiderMonkey shell and JSPL at least), MS/Wine ...


22

You can set set the TMOUT variable to a number in seconds that you wish for bash to wait before automatically logging out the shell if no command is run.


18

{ cat sample1.txt; tail -n +4 sample2.txt; tail -n +4 sample3.txt; } > out.txt


17

Brace expansion comes very handy when creating large directory structures: mkdir -p dir1/{subdir1,subdir2}/{subsubdir1,subsubdir2} This will give you find dir1 -type d dir1 dir1/subdir1 dir1/subdir1/subsubdir1 dir1/subdir1/subsubdir2 dir1/subdir2 dir1/subdir2/subsubdir1 dir1/subdir2/subsubdir2 You could even go one step further and put brace expansion ...


16

I use it when I want to reduce typing: geany /path/to/file1 /path/to/file2 # versus geany /path/to/file{1,2} Another example: wajig install libpam0g-dev libiw-dev libdb-dev # versus wajig install lib{pam0g,iw,db}-dev


16

When they are not quoted, $* and $@ are the same. You shouldn't use either of these, because they can break unexpectedly as soon as you have arguments containing spaces or wildcards. "$*" expands to a single word "$1c$2c...". Usually c is a space, but it's actually the first character of IFS, so it can be anything you choose. The only good use I've ...


16

When "SOMEPATTERN" starts or may start (for instance if it's a variable like "$PATTERN" which you don't have full control on) with a - (dash) character. Also with GNU grep (unless $POSIXLY_CORRECT is on), it's useful if other arguments (file names) may start with -. Alternatively, you can do grep -e -SOMEPATTERN- -- file1 file2 -xxx- -- marks the end of ...


15

For the sake of variety, here's another way with cut: cut -d \; -f -3


14

It's showing the contents of the special variable $@, in Bash. It contains all the command line arguments, and this command is taking all the arguments from the second one on and storing them in a variable, variable. Example Here's an exampe script. #!/bin/bash echo ${@:2} variable=${@:3} echo $variable Example run: ./ex.bash 1 2 3 4 5 2 3 4 5 3 4 5 ...


13

The [ command is an ordinary command. Although most shells provide it as a built-in for efficiency, it obeys the shell's normal syntactic rules. [ is exactly equivalent to test, except that [ requires a ] as its last argument and test doesn't. The double brackets [[ … ]] are special syntax. They were introduced in ksh (several years after [) because [ can ...


13

Here is a simple script to demonstrates the different between $* and $@: #!/bin/bash function test_param() { echo "Receive $# parameters"; echo Using '$*'; echo for param in $*; do echo "==>$param<=="; done; echo echo Using '"$*"'; for param in "$*"; do echo "==>$param<=="; done; ...


12

AProgrammer's suggestion of using xargs is often best, but another option is to use redirection into a while loop, which allows additional commands to be made and variables to be set: while read -r dir; do mkdir $dir; done < myfile An example of a more complicated structure would be: now=`date +%Y%m%d.%H%M%S` while read -r dir; do ...


12

The reason file.txt is empty after that command is the order in which the shell does things. The first thing that happens with that line is the redirection. The file "file.txt" is opened and truncated to 0 bytes. After that the sed command runs, but at the point the file is already empty. There are a few options, most involve writing to a temporary file. ...


11

I usually use the -exec utility. Like this: find . -type f -exec du -a {} + I tried it both on bash and ksh with GNU find. I never tried AIX, but I'm sure your version of find has some -exec syntax. The following snippet sorts the list, largest first: find . -type f -exec du -a {} + | sort -n -r | less


11

I'd do something like xargs mkdir < myfile


11

From help let: Exit Status: If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise.. Since var++ is post-increment, I guess the last argument does evaluate to zero. Subtle... A perhaps clearer illustration: $ let x=-1 ; echo x=$x \$?=$? x=-1 $?=0 $ let x=0 ; echo x=$x \$?=$? x=0 $?=1 $ let x=1 ; echo x=$x \$?=$? x=1 $?=0 $ let x=2 ; echo ...


11

Short answer: use "$@" (note the double quotes). The other forms are very rarely useful. "$@" is a rather strange syntax. It is replaced by all the positional parameters, as separate fields. If there are no positional parameters ($# is 0), then "$@" expands to nothing (not an empty string, but a list with 0 elements), if there is one positional parameter ...


11

You should reduce the columns output by ps to the minimum, i.e. request only the username here - this simplifies further processing. For example: $ ps -eo user= will print the owner of all the currently running processes (= suppresses the header). An easy way to get the counts for each user: $ ps -eo user= | sort | uniq -c 1 dovecot 1 messagebus ...


10

I use it to compare actual test output to desired test output during development. If test #41 fails, it's easy to see what the difference between the test output (in file tests.output/041) and the desired output (in file tests.out/041): $ diff tests.{out,output}/041


10

@enzotib has already pointed out what your syntax error is - I'm going to go off on a little tangent. Summing a column of numbers is one of those things that keeps popping up. I've ended up with this shell function: sumcol() { awk "{sum+=\$$1} END {print sum}" } With this, your solution becomes: ls -lrt | sumcol 5 That will sum the numbers in ...


10

At least in bash, as long as there are no filenames containing spaces and newlines, this: mkdir $(< myfile) works. So we have a useless use of for, xargs too. < does not start a new process in bash, in contrast to cat, but I don't know for ksh.


10

On linux you can use /proc/PID/exe. Example: # readlink /proc/$$/exe /bin/zsh


10

That's a ksh feature also found in bash and recent versions of zsh. In ksh and bash, you can access several elements of an array by using the ${array[@]:first:length} syntax, which expands to up to length (or all if length is omitted) elements of the array array (in the list of elements of the array sorted numerically on the indexes), starting with the ...


9

First of all, you should use straight single quotes ('), not the inclined ones (`). The awk inline script could be as follow: ls -lrt | awk '{ total += $5 }; END { print total }' so, no need to initialize total (awk initializes it to zero), and no need to loop, awk already executes the script on every line of input.


9

Try like this: java -version 2>&1 | grep version | awk '{print $NF}' Looks like the output is going to stderr. Also, grep is not needed: java -version 2>&1 | awk '/version/{print $NF}'


8

Your code looks like an entirely justified example of using tempfiles to me. I'd stay: stick with this approach. The only thing that really needs to be changed is the way you create the tempfile. Use something like TMP=$(tempfile) or TMP=$(mktemp) or at least TMP=/tmp/myscript_$$ This way you won't let the name be easily predicted (security) and ...


8

You can do this sort of thing with eval, built-in to many fine shells, including ksh: #!/usr/bin/ksh set $(iostat) myvar=6 eval "echo \$$myvar" The trick is to double-quote the string you feed to eval so that $myvar gets substituted with "6", and to backslash the outer dollar-sign, so that eval gets a string "$6". I got "%user" for the output, but I ...



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