50 votes

Replace backslash("\") with forward slash("/") in a variable in bash

The correct substitution is home_mf="${home//\\//}" This breaks up as follows: // replace every \\ backslash / with / slash Demonstration: $ t='\a\b\c'; echo "${t//\\//}" /a/b/c ...
Toby Speight's user avatar
  • 4,977
50 votes

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
47 votes

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
44 votes

Suppress execution trace for echo command?

When you are up to your neck in alligators, it’s easy to forget that the goal was to drain the swamp.                   — popular saying The question is about echo, and yet the majority of the ...
G-Man Says 'Reinstate Monica''s user avatar
24 votes

appending timestamp to tail -f results

It probably doesn't get any shorter than this: tail -f outputfile | xargs -IL date +"%Y%m%d_%H%M%S:L" Note that I used the timestamp format suggested by your question, but you're free to use ...
David Ongaro's user avatar
20 votes

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
19 votes

Write n bytes from a file into another in Bash

Yes. Per the dd man page, you are looking for something like: dd bs=1 count=60 if=_filename_1_ of=_filename_2_ dd bs=1 skip=60 count=40 if=_filename_1_ of=_filename_2_ where _filename_n_ is ...
cxw's user avatar
  • 1,719
17 votes

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
16 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
  • 171
16 votes

How to update bash on Mac OS X Yosemite

Install new bash: brew install bash Make this the default shell: chsh -s /usr/local/bin/bash Set the environment in a script: #!/usr/bin/env bash Using env will look for Bash in your $PATH and ...
Ben's user avatar
  • 261
16 votes

Bash variables with space (directories)

The problem is not with $file, it's with your usage of $(find…). Both regular variables and $(…) process substitutions are subject to exactly the same kind of word-splitting. If you put $(find…) in ...
u1686_grawity's user avatar
15 votes

How to change/add a tracker to a .torrent file using the command line?

You can edit the torrent files themselves using transmission-edit. Supposedly transmission-edit -a udp://tracker my.torrent should work. If you have already added the torrent file to transmission, you ...
Frederick Nord's user avatar
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 ...
Iskustvo's user avatar
  • 280
15 votes

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
14 votes

cp: cannot stat '/some/path/*': No such file or directory

When you use "$var*" the output will be /some/path*, where * is considered as a character (not as a wildcard, since it's inside the double quotes - see globs). So in your case (/some/path/*),...
Erlis D.'s user avatar
  • 808
13 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.5k
12 votes

Replace backslash("\") with forward slash("/") in a variable in bash

This will do it: home_mf=${home//\//\\} # forward to backward slash home_mf=${home//\\//} # backward to forward slash e.g.: $ cat slash.sh #!/bin/bash set -x home=/aa/bb/cc home_mf=${home//\/...
jlliagre's user avatar
  • 14.2k
11 votes

How can I compare a variable to a text string, rather than integer, in an if/else statement?

This method would also work. Very similar to @Guru's answer but removes the need for double square brackets. if [ "$act" == "add" ] then echo "Good!" else echo "Not good!" fi
Yokai's user avatar
  • 299
11 votes

Linux: How does file modification time affect directory modification time and directory access time?

Many questions, so I'm going to answer the "how does it work" part, not "how do I get this working." This is dependent on the file system used. I'm answering based on ext[234], others probably work ...
Sami Kuhmonen's user avatar
11 votes

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

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

There's no exact replacement. You could install the Linux watch command with your favorite package manager (homebrew or macports), or manually. Or you could roll your own watch-like functionality ...
Spiff's user avatar
  • 104k
10 votes

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
9 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
  • 1,972
9 votes

Write n bytes from a file into another in Bash

Here is another approach using head and bash command groups: { head -c60 > /dev/null ; head -c40 > output.txt ; } < input.txt The first head here reads the first 60 bytes from input.txt and ...
Digital Trauma's user avatar
9 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
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

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
  • 3,883
8 votes

'while read' loop through lines in text file losing characters in Bash script. is FFmpeg line to blame?

Use -nostdin option in ffmpeg. From FFmpeg documentation: Enable interaction on standard input. On by default unless standard input is used as an input. To explicitly disable interaction you need ...
llogan's user avatar
  • 59.7k
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

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
  • 20k

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