New answers tagged bash
0
votes
Is it a good practice to delete all variables at the end of a script?
Is it a good practice to unset all variables at the end of a script?1
It depends on your use case(s) and intention(s) because a shell script can be run in different ways (see "1. Ways to call ...
- 916
1
vote
Accepted
How to use trap with child tmux process
You need to replace the signal with SIGCHLD.
trap foo SIGCHLD
That signal gets sent to the parent if a forked child process stopped/terminated. While SIGHUP only affects the current process/script as ...
- 126
2
votes
Does running exec command preserves environment variables of the calling shell? If so, why?
Unix doesn't use spawn, it uses fork. Fork clones a process (with a very short list of differences in the child), typically immediately before an exec.
(As said in comment..) The exec system call is ...
- 5,081
0
votes
How to add a newline to the end of a file?
perl -0pe 's/\R?$/\n/' file
-0 without arguments is equivalent to no record separator (treat the whole file as a single line), so $ equals EOF not EOL.
\R is equivalent at CRLF (Windows) or LF (Linux)...
- 131
1
vote
Methodology to copy data from one machine to many others in a fast way
You should have a look to programs which use multicast or broadcast dataframes. Then the master won’t be much limited by the network bandwidth since all files will be transmitted once.
mrsync can be ...
- 1,484
2
votes
Methodology to copy data from one machine to many others in a fast way
If you can't use a network file system, then install and use GNU parallel to run multiple simultaneous rsync commands.
Tune the number of executors so as not to completely saturate bandwidth, since ...
- 888
-1
votes
How can I test if a particular alias is defined?
You can write like this
[[ $(type your_alias) == *"alias" ]] && echo "alias defined" || echo "alias undefined"
Example:
[[ $(type fd) == *"alias" ]] &...
- 1
-2
votes
for loop over directory glob with space won't work
Handling filenames with spaces is difficult in simple bash, use find instead. Read man find and use the -print0 option, along with -type d (for directories) or -type f(for files), or -type l (for ...
- 3,892
0
votes
using aliases in function?
Apart from the visibility issues of aliases usually only being defined in interactive shells, you apparently need to use eval to execute the alias if the alias name is in a shell variable.
...
- 11.5k
5
votes
Accepted
for loop over directory glob with space won't work
The problem you have does not stem from the whitespace in the directory names, but (ironically) from the remedy you applied, which is correctly quoting your shell variables.
The quoting you applied is ...
- 21.1k
2
votes
Accepted
How to kill master script from slave script?(in bash)
At least on Linux, you can have the child script kill the process group of the parent. That will kill the parent itself and any child processes it may have launched. From man kill:
ARGUMENTS
...
- 228k
1
vote
How to kill master script from slave script?(in bash)
Clearly hackish, but to answer the question: you can reach the parent bash script by $PPID.
$ cat > A.sh
#!/bin/bash
/bin/bash B.sh
echo "A continues"
exit 0
$ cat > B.sh
#!/bin/bash
...
- 101
1
vote
Accepted
Is it possible to paste value into editable string of terminal in key binding function?
I found it.
Man bash speaks about it in description of bind command. Editable string have variables: READLINE_LINE, READLINE_MARK, READLINE_POINT.
Then a command will be like this:
bind -x '"\C-...
- 11
0
votes
Accepted
How to use a path to a directory with a space within variable to cat all files within the directory?
I'd use find with its -exec option for this. For example:
TEST='path/with space/to/folder1/'
find "$TEST" -maxdepth 1 -type f -exec cat {} + > test
The -maxdepth 1 option prevents ...
- 73.6k
0
votes
set -o vi in prompt leaves insert mode every time I use arrow keys and has warning bell constantly (bash/ksh)
The value of the $TERM environment variable selects which terminfo entry is used by programs which interpret function key sequences and which control the cursor and the terminal screen with escape ...
- 783
0
votes
Accepted
Starting a string at some column number
Set the first string to a fixed length.
n=20
printf "%-${n}.${n}s %s\n" "$string1" "$string2"
- 3,672
1
vote
grep exact process name in bash script with variable argument
In shells that support it, you could use the substring expansion to put the brackets in the right places:
name=ffmpeg
ps -ef | grep "[${name:0:1}]${name:1}"
would run grep "[f]fmpeg&...
- 128k
0
votes
grep exact process name in bash script with variable argument
Would inverted-match with grep work?
grep -v 'grep\|script'
This will not match grep or script.
3
votes
Accepted
grep exact process name in bash script with variable argument
Simply run:
pgrep -fl script.sh
From man pgrep:
pgrep, pkill, pidwait - look up, signal, or wait for processes based on name and other attributes
-f, --full
The pattern is normally only ...
- 29k
3
votes
Accepted
Why is my entire home backed up?
There is a blank line in your list of files to backup. Inside the loop this means that $worth is empty, which in turn results in execution of this command:
cp -Rv /home/poor /run/media/poor/backup/
...
- 101k
2
votes
Accepted
How to remove citations when launching bash?
Found it. In my distro (Artix) it is here:
/etc/bash/bashrc.d/local.bashrc
I commented out the line with the fortune command, and no more famous sentences are shown when I open a console.
In case I ...
- 509
0
votes
Incrementing a number based on number of zeroes
The following awk program would produce two "rulers", each consisting of a line of digits. The second line will be numbered from 1 to 10 (with 0 taking the place of 10) with no intermediate ...
- 311k
1
vote
Accepted
Incrementing a number based on number of zeroes
This function makes the required repeats (up to 200 chars), then truncates them according to the argument.
#! /bin/bash
Ruler () { # (lth)
local Tens Digs j
printf -v Tens '%10d' {1..20}
...
- 7,280
0
votes
Lengthening a string using characters from another
If what you want is to right-pad $dk to the length of $ndk using characters from $symbols, in zsh:
$ dk=FIRST ndk=7 symbol='!@#$%^&'
$ printf '%s\n' ${(pr[ndk][$symbol])dk}
FIRST!@
$ ndk=20
$ ...
- 504k
0
votes
Accepted
Lengthening a string using characters from another
df=3
dk=hello
symbol=123456
dk+=${symbol:0:df}
This appends the $df first characters from $symbol to the end of the string in $dk.
The value in $dk at the end of the above code would be hello123.
...
- 311k
1
vote
Console doesn't render according to man manual
It is not man who does not print italic, it is a formatter roff which is to blame. By historical reasons.
the text modes were developed in the ancient times, long before graphics was available, and ...
- 3,895
0
votes
Accepted
How to delete all text before and when string occurs on sed
Using GNU sed, and assuming your file is small enough to fit in memory, you can do this:
$ sed -z 's/.*jum//' file
ps over
the lazy
dog
The -z tells sed to treat its input as NUL-separated, meaning ...
- 228k
0
votes
How to delete all text before and when string occurs on sed
With awk:
$ awk 'sub(/^jum/, ""){f=1} f' ip.txt
ps over
the lazy
dog
If the substitution succeeds, set a flag to print the lines.
- 11.3k
4
votes
curl for website without intermediate certificate
curl handling of this case depends on the build.
curl can use any of several TLS implementations, determined at compile time, and it should not be much of a surprise that the behavior of the TLS ...
- 3,703
0
votes
WSL2 Ubuntu: How to start ssh-agent and add ssh key automatically on each terminal start up
Hum... you try to load ~/id_rsa. Usually, keys are in the ~/.ssh folder. AFAIR ssh is also very picky with file and folder permissions. If a key is in a directory that ssh thinks not secured enough, ...
- 251
1
vote
Accepted
ssh EOF in bash script - how to store value of command?
Fixing up the current way of doing things
ssh $USER@server.com << EOF
cd /data/dev/${ARK_PATH}/
./gradlew bootRun --no-daemon
LATEST_FOLDER=\$(find . -type d -exec stat --printf="%...
- 17k
2
votes
How to delete all text before and when string occurs on sed
sed -ne '1,/^jum/{s/^jum//p;d;}' -e p
As @Sundeep states in comment, if you have GNU sed, use preferably
sed -ne '0,/^jum/{s/^jum//p;d;}' -e p. This would work even if jum on the first line. which is ...
- 251
0
votes
Help with -maxdepth in 'find -exec mv' command line
Sounds like you'd first want to move all directories that match, then all files, symlinks, … that remain and match.
You can do that by specifying -type d for directories on a first find run, and '(' -...
- 15.4k
1
vote
curl for website without intermediate certificate
curl can handle self-signed certificates.
You just have to type curl --cacert fullchain.pem URL where fullchain.pem is the certificate you have downloaded trought a trusted path.
If you don't feed ...
- 1,484
2
votes
How to move every three day file based on the filename to respective folders in bash
In zsh, you could do it using its zmv batch renamer.
autoload -Uz zmv
mkmv() { mkdir -p -- $2:h && mv -- "$@"; }
zmv -n -P mkmv '(<->.)(<1-366>).*.sac' \
'$...
- 504k
-1
votes
Bash - while read line fails when ffmpeg executed in while body
Maybe a little late, but the problem with ffmpeg is that it reads from standard input as well, consuming data meant for read in the while-read-loop.
I had some strange timestamp messup. To solve this ...
- 1
0
votes
Meaning of uint32:0 in dbus-send and bash syntax
From the man page (D-Bus 1.14.6):
NAME
dbus-send - Send a message to a message bus
SYNOPSIS
dbus-send [--system | --session | --bus=ADDRESS | --peer=ADDRESS] [--sender=NAME] [--dest=...
- 115
1
vote
Is it possible to create such a tool that allows to save a being read text from a PDF reader into a string for handling this text afterwards?
Ghostscript can extract the text from a PDF file:
gs -sDEVICE=txtwrite -sOutputFile=myfile.txt -dBATCH -dNOPAUSE myfile.pdf
- 1,484
2
votes
Accepted
How to move every three day file based on the filename to respective folders in bash
Everything can be done much simpler if using bash's abilities:
#!/bin/bash
for file in *.sac
do
year=${file:0:4}
day=${file:5:3}
range_start=$(( (($day-1)/3)*3+1 ))
range_end=$(( $...
- 3,895
3
votes
Accepted
why can't I use the 'z' command inside a bash function?
According to the source of that z, it's meant to be an alias. Aliases are a bit like C preprocessor macros, they're not command or functions. They're from the C shell which doesn't have functions, and ...
- 504k
0
votes
Difference between ' and ` in bash script
Double quotes preserve the value of each character enclosed in the quotes, except for backticks, dollar sign and backslash.
Single quotes preserve the value of each character enclosed in the quotes ...
- 2,001
0
votes
Glob pattern for replacing two or more spaces followed by some text within a string
The confusing thing about character classes like [:blank:] is that they go inside of bracket expressions, so the whole thing has two concentric sets of brackets: [[:blank:]]. This also means you can ...
- 4,252
1
vote
Accepted
Glob pattern for replacing two or more spaces followed by some text within a string
For two or more of something, with glob patterns, you can use:
{2,}(something) in ksh93
(something)(#c2,) in zsh -o extendedglob.
something(something)## in zsh -o extendedglob.
something+(something) ...
- 504k
0
votes
Glob pattern for replacing two or more spaces followed by some text within a string
Brief: In Pure Bash: out=${str/* /}
Long:
str="Here is a list of functions Marionette Recutils"
echo "${str/* /}" # <-- This way
# Out: Marionette Recutils
From man bash / ...
- 121
0
votes
Find files and delete x from total of the list
This gets you a list of all but the youngest limit_files files, which you can then remove.
FileList=`ls -1rt ${ZK_PATH}/*log* | head -${limit_files}`
rm $FileList
It won't work with files that ...
- 888
1
vote
Glob pattern for replacing two or more spaces followed by some text within a string
I'm fairly certain this will not handle several edge cases, but I was successful with this...
# echo "Here is a list of functions Marionette Recutils" | sed "s/ .*//"
Here is a ...
- 11
1
vote
Use .bashrc be used to modify execution of /usr/bin/env based on PWD
You could write a wrapper for /usr/bin/env that does this:
mv /usr/bin/env /usr/bin/env.bak
cat <<-EOF >/usr/bin/env
#!/usr/bin/bash
export PATH="${PWD}:${PATH}"
/usr/...
- 187
2
votes
Colouring substrings
Is this what you are looking for?
#!/bin/bash
glow() {
l=$1
r=$2
str=$3
glow_on='\E[37;46;1m'
glow_off='\E[0m'
strl=${str/$l/$glow_on$l}
strlr=${strl/$r/$r$glow_off}
...
- 3,895
1
vote
Accepted
Colouring substrings
In recent-enough Bash (ones supporting patsub_replacement, which might mean 5.2), something like this might work:
color_me_blue() {
blue=$'\e[1;34m'
normal=$'\e[0m'
pattern="$1"
...
- 128k
0
votes
SUSE 12 zypper can't install unsigned package non interactively
You can use the --no-gpg-checks flag
sudo zypper --no-gpg-checks install UNSIGNED_RPM_PATH_HERE
Top 50 recent answers are included
Related Tags
bash × 26402shell-script × 6615
shell × 4024
linux × 2737
scripting × 1499
awk × 959
command-line × 936
sed × 922
terminal × 756
text-processing × 688
grep × 682
ssh × 667
find × 627
variable × 608
ubuntu × 555
io-redirection × 551
pipe × 535
zsh × 529
files × 521
environment-variables × 510
quoting × 492
alias × 483
command-history × 440
wildcards × 435
regular-expression × 413