Questions tagged [bash]
Questions specific to GNU’s Bourne Again SHell, as opposed to other Bourne/POSIX shells. For questions about Unix shells in general, use the /shell tag instead. For shell scripts with errors/syntax errors, please check them with the shellcheck program (or in the web shellcheck server at https://shellcheck.net) before posting here.
27,267 questions
3
votes
5
answers
220
views
How can I use sed to chain append lines from a text file, add it as a suffix to the text on the same lines numbers on another file, and so on?
I've tried using sed for this. I've tried putting the lines of interest in variables as well.
I have two examples I want to achieve for now. Lets say I have thousands of urls in a file called links....
3
votes
3
answers
876
views
How do I ensure a bash script argument is a specific value?
The following script is supposed to check $1 and see if it is a specific value. It only works when one Bash [[... =~ ...]] regex check is passed in an if statement.
The error is: When more than one ...
1
vote
1
answer
35
views
White spacing is causing variable to not contain all text in result
Disclaimer: I am brand new to bash
I'm building a dialog box and trying to get some Docker commands to populate results.
I am using this to return a result, which works for the most part.
result=($(...
0
votes
2
answers
133
views
Process sed capture group with a bash function before replacement: "sh: 1: <bash function>: not found"
So, I was playing around with this answer, when I found that neither
printf_stdin () {
read input
printf "$input"
}
sed "/lorem ipsum foobar/ {
s/'/'\"'\"'/g
s/\...
2
votes
1
answer
35
views
Shell bracket list wildcard doesn't work with variable [duplicate]
The following works:
ls {000/487,000/488,000/489,000/490,000/491,000/492}
...many files being listed
But this doesn't. Why ?
LIST=000/487,000/488,000/489,000/490,000/491,000/492
ls {$LIST}
ls: cannot ...
1
vote
0
answers
53
views
Running XFCE on Debian Trixie. Bash history is not saving any sudo commands
Bash history works OK for except for sudo commands.
Code from .bashrc follows.
How can I modify the behaviour to include anything starting with sudo?
NOTE. This behaviour is the same with a brand new ...
2
votes
1
answer
95
views
Bash: function in a script with flags
Using https://stackoverflow.com/a/7948533/31298396, we can implement flags for a script testing as follows:
#!/bin/bash
# Flags
# Source: https://stackoverflow.com/a/7948533/31298396
TEMP=$(getopt ...
0
votes
1
answer
64
views
Bash Script Linux - combines a QUIET function with other functions without hiding some of them
#!/bin/bash
DefaultColor="\033[0m"
GREEN="\033[32m"
RED="\033[31m"
PURPLE="\033[35m"
YELLOW="\033[33m"
CYAN="\033[36m"
CmdOk="\033[...
5
votes
1
answer
373
views
locking scripts using flock - can same file descriptor be used concurrently?
Common method for preventing concurrent runs for a given script is utilizing flock as:
LOCK=/var/lock/my-lock
exec 9>>"$LOCK"
if ! flock --exclusive --nonblock 9; then
echo "...
6
votes
2
answers
331
views
suppress stderr when inputting a file to a variable
var="$(cat no-exists)"
var="$(< no-exists)"
give a similar output:
cat: no-exists: No such file or directory
bash: no-exists: No such file or directory
I'll describe the ...
4
votes
3
answers
218
views
Get Character Code for Character in File Name
Short version: I am at a bash command line. In some directory, I can ls various files and see their names. I would like to get something like the ASCII character code for each character in a file's ...
-5
votes
1
answer
86
views
To have ., the source synonym, workable [closed]
The bash' command
source
has been modified in one of few ways:
alias
function overriding
or else
Then how can its synonym:
.
got being so too (problem is it's not letter) ?
2
votes
1
answer
243
views
x2 sudo commands cannot be executed in terminal
I am running a Ubuntu system on my Radxa Zero 3E (single board computer)
Using its GPIO I am driving a 5V LED via an NPN transisitor. Everything works.
For switching it on I use gpioset $(gpiofind ...
1
vote
3
answers
181
views
Execute bash commands one at a time
I have a list of bash commands that I'd like to execute one at a time. They could be in a file, or the terminal, or whatever is necessary. But I'd like to execute the first one then I'd check the ...
15
votes
2
answers
2k
views
Why does "seq 1000000 | tee /dev/stdout" produce more single-digit numbers than expected?
I'm in Linux and in Bash. I expect
seq 1000000 | tee /dev/stdout
to always output 9 numbers from 1 to 9 and their duplicates (18 in total). But when I do
seq 1000000 | tee /dev/stdout | grep -c '^[1-...
10
votes
5
answers
605
views
Grep with output of not the complete line
With grep I can filter lines.
But if the lines are pretty long it gets messy.
How can I only get "some chars around" my search-string?
f.txt
this is a red cat in the room
this is a blue ...
4
votes
1
answer
835
views
Why does bash need file descriptor duplicating?
I found out I don't totally understand how bash file descriptors work. I would like to have some guidance here.
I have a scenario where I need to read the content of a file, and for each line of the ...
0
votes
0
answers
29
views
can't remove items from the bash history using a while loop [duplicate]
I have a faulty command several times in my history and want to remove it with history -d <id_of_faulty_command>.
I can identify this via a --writeout (instead of the correct --write-out)
Now ...
-1
votes
2
answers
104
views
Unix folder has file named "-f". Standard `/bin/bash` tools do not recognized this (can not `rm`, `cat`, nor `stat` this). What to do? [duplicate]
Welcome to Termux
Docs: https://doc.termux.com
Community: https://community.termux.com
Working with packages:
- Search: pkg search <query>
- Install: pkg install <package>
- ...
12
votes
2
answers
1k
views
Why arithmetic syntax error in bash causes exit from the function?
I was debugging one script, and discovered unexpected behavour of handling arithmetic syntax error.
Normally, when error happens, script is just continuing execution.
#!/bin/bash
func2 () {
echo &...
2
votes
1
answer
67
views
Make `set -o vi` vim cursor reflect editing mode when in `read`
I have a script read.sh:
read -ei "hello world"
eval $REPLY
Upon executing chmod +x read.sh and then running ./read.sh, I notice that pressing Esc followed by b or w jumps between the ...
0
votes
0
answers
37
views
Bash replacement with new line [duplicate]
The command echo ${PATH//:/$'\n'} should replace colons with new lines. However, I am getting spaces. What am I doing wrong here?
1
vote
2
answers
115
views
How to have bash logging ("set -x") only log the main command line (not split into pipelines)?
Consider this one-liner bash example:
$ (function test() { echo "testing a string" | grep "a" | sed 's/i/y/g'; }; set -x; test | grep "test" )
+ grep --color=auto test
+ ...
0
votes
1
answer
66
views
command line access to bash directory stack [duplicate]
In the tcsh shell, the elements of the directory stack can be access on the command line using =<#>, e,g, =0 =1 .... and a sampled command like:
cp Something =1/
would copy file Something to ...
7
votes
1
answer
1k
views
Mysterious prompt for sudo password while opening new shell
There is a remote machine which I am accessing through ssh and every time I open a new shell either via login or tmux, I get asked for a sudo password.
I have checked the .bashrc and /etc/bash.bashrc ...