Skip to main content
50 votes
Accepted

What is <<!END in a Bash script?

It's a Here Document -- the lines between <<!END and !END are fed to the stdin of wlst.sh It could also be expressed as: echo "connect('user','pw'); p=redeploy('application'); p.printStatus();...
glenn jackman's user avatar
48 votes
Accepted

Is it safe to open a file that is being written by a running script?

Reading the file is safe, although double clicking you mentioned will probably open the file in some editor that will offer you an option to make changes and save them. Missclicks happen, so I ...
Kamil Maciorowski's user avatar
21 votes
Accepted

Dynamically append text to filenames in Bash

These solutions you link to are in fact quite good. Some answers may lack explanation, so let's sort it out, add some more maybe. This line of yours for file in *.txt indicates the extension is known ...
Kamil Maciorowski's user avatar
21 votes
Accepted

Get bash to respect quotes when word splitting subshell output

Bash really doesn't have a good way to parse a string into substrings, while respecting quotes. Whether it's coming from a command expansion (that is, $( ) -- what I think you're calling a subshell) ...
Gordon Davisson's user avatar
20 votes
Accepted

When pressed ctrl+c only break out of current function and not whole bash script

Analysis When you hit Ctrl+c, the line discipline of your terminal sends SIGINT to processes in the foreground process group. Bash, when job control is disabled, runs everything in the same process ...
Kamil Maciorowski's user avatar
17 votes

Edit xml file using shell script / command

xmlstarlet edit --update "/book/fiction[@type='b']/author/@type" --value "Local" book.xml
Kit's user avatar
  • 181
15 votes

Insert quote into string variable in bash

In bash you can use \ as an escape character what whatever follows it. In your case, use it like this: string="\"desktop/first folder\""
jcbermu's user avatar
  • 17.9k
15 votes

Is it safe to open a file that is being written by a running script?

As long as you are not writing to it, it should be okay. However, I would recommend using tail -f log_file in another terminal. This command will "follow" the file log_file and write the newly ...
Iskustvo's user avatar
  • 370
15 votes
Accepted

What is awk '{ print $2; }' doing exactly?

ps aux lists all processes. grep 'sidekiq 5' only displays those lines from the list that contain the string "sidekiq 5". grep -v grep removes those lines that contain the string "grep&...
berndbausch's user avatar
14 votes

How to compare numeric strings in Bash?

No need for Python, awk or bc etc. Bash can strip off the minor part of the version number: $ cat t.sh #! /bin/bash DEBVERS=12.11 echo "DEBVERS = " $DEBVERS major=${DEBVERS%.*} echo "...
RedGrittyBrick's user avatar
11 votes

how to see git log automatic refresh

You can make that a one-liner with watch: watch --color -n 3 git log --all --decorate --oneline --graph --color=always Tweak the -n flag to change the refresh rate.
sdeleon28's user avatar
  • 111
11 votes
Accepted

Add a new element to existing JSON array

Do not try to parse JSON text with standard text processing tools like awk, sed or without JSON modules in perl, as they are non JSON syntax aware. jq is a lightweight JSON processor that allows you ...
Inian's user avatar
  • 250
10 votes

how to gzip and scp at the same time

In case you need to get the files/directories from a remote server, into a local archive, you can use tar + gzip inside ssh, and redirect to a local file. For example: ssh user@server "sudo tar cvzf -...
Noam Manos's user avatar
  • 2,302
9 votes
Accepted

Insert quote into string variable in bash

If you don't do variable substitution, using single quotes as delimiters, so you can use double quotes in your string: string='"desktop/first folder"'
xenoid's user avatar
  • 10.7k
9 votes

Remove duplicate files by comparing them with MD5 RECURSIVELY

I'd recommend something like the following instead: find . -type f \ | xargs md5sum \ | sort -k1,1 \ | uniq -Dw32 This will list all duplicated files in groups of files that have an ...
Attie's user avatar
  • 20.9k
9 votes
Accepted

Test condition in if command

You may know it or not, but let's make it clear: [ is not a part of the if syntax. I mean if [ … behaves like if true, if false or if any_command, where [, true, false and any_command are commands. ...
Kamil Maciorowski's user avatar
9 votes

Bash script to toggle monitor input from HDMI to DisplayPort and vice-versa

I am the developer of ddcutil How feature x60 (Input Source) behaves varies by monitor. Some monitors accept commands only from the current input source, others accept input from any source. Given ...
Sanford Rockowitz's user avatar
9 votes

How do I run a sudo command on a remote machine using ssh?

Changing the behavior of ssh When you run ssh without a command and there is a local pseudo-terminal, the tool allocates a pseudo-terminal on the remote side automatically. Usually you access an ...
Kamil Maciorowski's user avatar
9 votes
Accepted

Ffmpeg: suppress warning when writing to a single image

Either ffmpeg -i "src.png" -vf "$vf" -vframes 1 "dst.png" or ffmpeg -i "src.png" -vf "$vf" -update true "dst.png" The former is preferable,...
Gyan's user avatar
  • 39.5k
9 votes
Accepted

Bash [[ test =~ regex ]] vs perl command result

There are several different types of Regular Expression, each one adding more operators (and therefore requiring more characters to be escaped if they are to be considered literals). The =~ operator ...
Chris Davies's user avatar
  • 5,025
8 votes

Which bash rc files are run on non-interactive/non-login shells?

The answer to your specific question is that often only /etc/bash.bashrc (or /etc/bashrc) is loaded. There seem to be a couple of ways to address this, most are workarounds unfortunately. In no ...
KCD's user avatar
  • 500
8 votes
Accepted

Cannot scp Over a Shared Connection

ssh and scp use the -S option for different purposes. ssh: -S ctl_path Specifies the location of a control socket for connection sharing, or the string “none” to disable connection sharing. Refer ...
Kenster's user avatar
  • 8,690
7 votes

Is there a native alternative to the `watch` command; for Darwin/OS X

Based on @Spiff awesome answer, I improved it a bit to avoid flickering/flashing on each execution: fakewatch () { while true; do DATE=$(date); RESULT=$(${@}); clear; echo "$DATE"; echo "$RESULT"; ...
Jorge Chip's user avatar
7 votes

What commands would I use to create two child processes serially?

I'm not sure there is an elegant command / answer to the question you asked (i.e. reach a certain point in its execution) - but there are tools / techniques which can be used to bodge up a solution, ...
davidgo's user avatar
  • 73.8k
7 votes
Accepted

What commands would I use to create two child processes serially?

You can use the xargs utility for this. It can run a command for each line on stdin, so we just need to ensure that xargs gets as input a single line on stdin at the time it should start the command, ...
Philipp Wendler's user avatar
7 votes
Accepted

sh syntax to handle zero files matching a wildcard, as well as more?

The simplest portable way to do this is to skip the loop for if the expansion doesn't produce something that actually exists: for f in *.ext1 *.ext2; do [ -e "$f" ] || continue handle "$f" done ...
Gordon Davisson's user avatar
7 votes
Accepted

How can I get the result of a command and its return code at the same time in a Bash script?

The problem here is that in the pipeline cat "$filename" | wc -l, when the file doesn't exist cat will exit with an error, but wc -l will successfully count the 0 lines of text it receives from cat. ...
Gordon Davisson's user avatar
7 votes
Accepted

How to unzip a file with a hyphen at the beginning of the file name

Try this method of prepending - stuff.zip with ./: unzip -l './- stuff.zip' Or use this method that does’t need quotes — single or double — but escapes the space after the hyphen: unzip -l ./-\ stuff....
Giacomo1968's user avatar
  • 59.1k
6 votes
Accepted

how to see git log automatic refresh

Based on this answer from SO, you can stop the need to quit by adding --no-pager immediately after the git. It's not the log command that is waiting for q, it's the less tool that is doing the ...
Tom Carpenter's user avatar
6 votes

What commands would I use to create two child processes serially?

This is just an idea, but have the first process pipe its output to grep. Have grep pipe its output to a read line loop, and compare each line with what you're looking for. Once found, execute proc2. ...
Louis Waweru's user avatar
  • 25.5k

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