Tag Info

Hot answers tagged

15

Easy trick for alias in $(compgen -a); do type $alias; done


13

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

What's important to understand is that ~ expansion is a feature of the shell (of some shells), it's not a magic character than means your home directory wherever it's used. It is expanded (by the shell, which is an application used to interpret command lines), like $var is expanded to its value under some conditions when used in a shell command line before ...


12

In any version of Bash on any system, yes. ~ as a term on its own is defined to expand to: The value of $HOME so it will always be the same as whatever $HOME is to the current shell. There are several other tilde expansions, such as ~user for user's home directory, but a single unquoted ~ on its own will always expand to "$HOME". Note that the ...


9

The construction <(tac file) causes to shell to: Create a pipe with a name On systems such as Linux and SysV which have /dev/fd, a regular pipe is used, and /dev/fd/<the-file-descriptor-of-the-pipe> is used as the name. On other systems, a named pipe is used, which requires creating an actual file entry on disk. Launch the command tac file and ...


8

Very simple: for i in *; do echo "<$i>" done This uses bash's file globbing. A sort is not necessary as bash already sorts pathname expansions. From man bash: Pathname Expansion After word splitting, unless the -f option has been set, bash scans each word for the characters *, ?, and [. If one of these characters appears, then ...


8

This is what Bash while loops do: while /path/to/application.app do : done It will run the application, and if it terminates successfully run the body of the loop. : is the shell's no-op command (the loop has to have a body, so that's what we put there); after that it goes back to the top and runs the program again. If it fails, the loop stops running ...


7

You can almost definitely just do: alias >>./bash_aliases


7

First, note that the -z test is explicitly for: the length of string is zero That is, a string containing only spaces should not be true under -z, because it has a non-zero length. What you want is to remove the spaces from the variable using the pattern replacement parameter expansion: [[ -z "${param// }" ] This expands the param variable and ...


6

It's because the return value of (( expression )) is not used for error indication. From the bash manpage: ((expression)) The expression is evaluated according to the rules described below under ARITHMETIC EVALUATION. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. This is exactly equivalent to ...


5

You're basically wanting to reset the terminal color right before bash executes the command. This can be done with a trap. For example: trap '[[ -t 1 ]] && tput sgr0' DEBUG Bash executes the DEBUG trap immediately before the command, so this will result in tput sgr0 (which resets formatting attributes) being run before each command. The [[ -t 1 ...


5

You could use the $? variable to get the return code of wget. If it's non-zero then it means an error occured and you tally it up until it reached a threshold, then it could break out of the loop. Something like this off the top of my head #!/bin/bash threshold=0 for x in {90..110}; do wget ...


5

If you're happy with a loop: for url in 'http://www.iqandreas.com/sample-images/100-100-color/'{90..110}'.jpg' do wget "$url" || break done That will run wget for each URL in your expansion until it fails, and then break out of the loop. If you want two failures in a row it gets a bit more complicated: for url in ...


5

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


5

This might be the ugliest Bash code I've ever written, but... IPv4_first=1.1.1.1 IPv4_second=2.2.2.2 IPv4_third=3.3.3.3 names=(${!IPv4_@}) eval "IPv4_all=(${names[@]/#/$})" printf "'%s'\n" "${IPv4_all[@]}" Look Ma, no loop! ${names[@]/#/$} prepends $ to the start of every element of the array, by matching an empty string anchored to the start of each ...


5

Shell scripts are normally treated as if they were the same as any other kind of executable file, such as binaries, Python scripts, Perl scripts, or any other kind of script. They have a shebang at the top that directs the kernel to execute them through the shell. They are expected to be invoked the same way as any other command. As such, a new shell is ...


5

Press Ctrl-Alt-e with a command using your aliases written (ready to run) and Bash will expand it. Ctrl-Alt-e is the default binding for the shell-expand-line readline command. Each time you push Ctrl-Alt-e Bash will expand one layer of alias, so push it repeatedly until your command is expanded as far as you need. If your Meta key is not Alt, substitute ...


5

The reason is as pmos writes above. One solution would be to use ((++n)) to do increment. Your expression will never evaluate to zero, and so never look like it causes an error.


5

You can do: sudo !! Another good one is alt ., to insert the last parameter of the previous command


4

while [ $X -lt "$1" ] is being evaluated even when there is no $1. Move the rest of the code into an else block so this doesn't happen. function random-word { # from linuxconfig.org if [ $# -eq 0 ] then echo "I need an argument, dummy" # To be extra friendly, give them a random word. echo "Here's a random word:" ...


4

In bash you can set nocaseglob: shopt -s nocaseglob for file in "$arg"/**/*.{txt,h,py} do .... done shopt -u nocaseglob noclaseglob is fine to use in any bash since 2.01, however ** requires bash or later (an it follows symlinks up to bash 4.3). Note the correction to quote $arg since there will be problems if this contains spaces or glob characters. ...


4

What is difference between | and <<()? There is a difference between them: | cause each command run in a separated subshell. <() run the command, which is substituted in background. For the next two question, we will do some strace: pipe: $ strace -fc bash -c 'tac /usr/share/dict/american-english | grep qwerty' $ time seconds ...


4

You're backgrounding the application, and the application is generating output. Your prompt is still there, it just has extra stuff being shown. For example: $ ( sleep 1 && echo hello ) & [1] 24764 $ █ And then after a 1 second delay, I get: $ ( sleep 1 && echo hello ) & [1] 24764 $ hello █ The echo is just writing output to ...


4

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.


4

I'd suggest a different approach, avoiding the possible word-splitting issues of ls #!/bin/bash shopt -s nullglob for ext in jpg png gif; do files=( *."$ext" ) printf 'number of %s files: %d\n' "$ext" "${#files[@]}" # now we can loop over all the files having the current extension for f in "${files[@]}"; do # anything else you like with ...


3

Not sure what you mean. Possibly with GNU grep: grep -Ero '(\\x[[:xdigit:]]{2})+' . To match strings of the format \xNN (the 4 characters backslash, x and two hexadecimal digits)


3

Did you remember to source your ~/.bashrc file after making the changes? Because the changes take effect in your .bashrc file after restarting your computer or simply sourcing the file.


3

If understand what you're wanting, using read should accomplish your goal. However you don't want the read to sit there blocking indefinitely, so you pass a timeout. For example: #!/bin/bash sleep 5 echo done sleeping read -N 10000000 -t 0.01 You can type whatever you want while sleep is running. Once it finishes, bash will invoke the read which will ...


3

Return needs to be an integer, so 1 or 0. In bash, false would be 1 and true would be 0. So I would replace 'N' with 1 and 'Y' with 0 Also, then you would also change the if statement as you would no longer be matching against 'Y', you would be matching against 0. if [ 'Y' == $(is_xyz_node $1) ] should become if is_xyz_node $1 as the function ...



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