Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.

All Questions

Tagged with or
Filter by
Sorted by
Tagged with
3 votes
3 answers
646 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 ...
EmberNeurosis's user avatar
4 votes
1 answer
158 views

Choice of field separator affects sort's ordering

Suppose we have a script named test_sort in our $PATH with the following contents: #!/bin/bash function echo_text () { printf -- "%s\n" "$fc$oc$fs$lc" printf -- "%s\n&...
Melab's user avatar
  • 4,458
1 vote
1 answer
30 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=($(...
James Durand's user avatar
0 votes
1 answer
34 views

Sorting output from find like tree's output

Here is a sample directory tree as it would appear if it were sorted in character code order (i.e., directories are not listed first): ${PREFIX}/ .bashrc .include.sh.d/ common.sh ...
Melab's user avatar
  • 4,458
0 votes
2 answers
128 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/\...
Grass's user avatar
  • 145
0 votes
1 answer
38 views

`xargs sh -c` not picking up last argument (vs `for i in $(...)`) [duplicate]

I just wanted to build a simple bash script that inverted a 'hex color code' (made up of 3 pairs of 2-digit hexadecimal numbers, in the format #RRGGBB). In my first code block I attempt to perform ...
Signor Pizza's user avatar
2 votes
1 answer
34 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 ...
dargaud's user avatar
  • 659
11 votes
1 answer
1k views

Is there any difference between [[ -n $1 ]] and [[ $1 ]] in bash?

The test [[ -n $1 ]] yields True if the length of string is non-zero. But I've seen elsewhere and have tried using just [[ $1 ]] without the primary -n and it seems to have the same effect. Is there ...
CertainPensioner's user avatar
1 vote
0 answers
50 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 ...
imazed's user avatar
  • 11
1 vote
1 answer
66 views

Bash: only the first long option is being processed

Suppose we have the file ./testing with contents #!/bin/bash # Flags # Source: https://stackoverflow.com/a/7948533/31298396 TEMP=$(getopt -o ''\ --long first,second \ -...
Grass's user avatar
  • 145
2 votes
1 answer
92 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 ...
Grass's user avatar
  • 145
5 votes
2 answers
532 views

Why doesn't the pwd nullary built-in error when provided an argument, whereas the nullary/unary exit built-in does error?

If I enter pwd foo bar at the prompt of the bash shell, the foo bar part of the command line seems to be completely ignored. If I enter exit 0 foo, I get bash: exit: too many arguments. So pwd ignored ...
Enlico's user avatar
  • 2,362
0 votes
0 answers
30 views

dbus-send array:string fails with elements containing spaces [duplicate]

I am trying to run the following bash script: #!/usr/bin/env bash declare -A WM_CLASSES=( ["ALACRITTY"]="Alacritty" ["AUDACIOUS"]="Audacious" [&...
Ahmad Ismail's user avatar
  • 3,134
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[...
Maxime's user avatar
  • 3
0 votes
2 answers
44 views

Source file in bash to set prompt with ANSI colors

I have a file named .ps1, and I'm running source .ps1 in bash to try and set my prompt up with color. The contents of the .ps1 file are: __prompt_command() { local EXIT="$?" local ...
iLiekTaost's user avatar
5 votes
1 answer
371 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 "...
basher's user avatar
  • 71
0 votes
1 answer
93 views

Why does Firefox 'copy as cURL' not download any file not download anything here?

I tried to do this: curl "https://imslp.org/wiki/Goldberg-Variationen%2C_BWV_988_(Bach%2C_Johann_Sebastian)" | perl -nle 'print "$1" while /<span id="num-of-ratings-[0-9]{6}...
Signor Pizza's user avatar
6 votes
2 answers
329 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 ...
Mario Palumbo's user avatar
0 votes
2 answers
63 views

Cleaning-up previous kernel RPMs after reboot

I have a few RHEL 8/9 boxes with a cron job that updates them automatically every week. The reboots are done manually from time to time and I would like to write a script to simplify the removal of ...
Fravadona's user avatar
  • 1,601
4 votes
3 answers
215 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 ...
ASV's user avatar
  • 43
-4 votes
3 answers
147 views

How to make this command prompt?

I want this command prompt. Any ideas?
Danijel's user avatar
  • 224
-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) ?
user17227456's user avatar
0 votes
1 answer
36 views

How to set a bind -V variable from the bash command line, not ~/.inputrc? [duplicate]

$ bind -V | grep horiz print-completions-horizontally is set to `off' How do I set it to `on' from the bash command line? I know how to set it in ~/.inputrc. That's not my question.
Dan Jacobson's user avatar
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 ...
nightcrawler's user avatar
4 votes
2 answers
290 views

Custom separator in ps

I have this integrated in a bash-script on an outdated Debian 10. ps -axo "%p ;;; %a" Works well, output is like 10161 ;;; [kworker/0:1-cgroup_destroy] 12173 ;;; [kworker/2:0-events] 12379 ;...
chris01's user avatar
  • 1,039
1 vote
3 answers
180 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 ...
Mike's user avatar
  • 290
-9 votes
1 answer
105 views

"File name too long" when printing a long base64 string. Why?

Downloading an image and encoding it into base64: curl -o output.jpg https://upload.wikimedia.org/wikipedia/commons/thumb/f/f6/USA-San_Jose-De_Anza_Hotel-3.jpg/500px-USA-San_Jose-De_Anza_Hotel-3.jpg ...
Franck Dernoncourt's user avatar
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-...
decision-making-mike's user avatar
7 votes
1 answer
362 views

Cannot delete bash associative array element

Consider an associative array in bash (versions 5.2.15(1)-release and 5.2.21(1)-release): declare -A ufs=(); ufs["one"]=1; ufs["two"]=2; ufs["three"]=3 printf '> %s\n' ...
Chris Davies's user avatar
10 votes
5 answers
603 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 ...
chris01's user avatar
  • 1,039
4 votes
4 answers
452 views

How can I find common prefixes in file names to group them?

I would like to be able to find all files in multiple directories whose file names start with the same string, but preferably not if that string is only one word or contains fewer than perhaps 5 ...
EmmaV's user avatar
  • 4,417
4 votes
1 answer
833 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 ...
sylye's user avatar
  • 718
1 vote
2 answers
101 views

Use numbered file descriptors with SSH

I need to pipe data through SSH to a command that reads stdin. The command needs to run with sudo so I need to be able enter the sudo password, too. Without ssh, I'd use numbered file descriptors like ...
con-f-use's user avatar
  • 434
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 ...
vrms's user avatar
  • 287
2 votes
1 answer
211 views

cal or ncal - wrap more months?

Using cal and ncal, I know that I can display a certain number of months using various flags (for example, ncal -C -A 4 will show the current month and the 4 months after). What I am trying, and so ...
m_mlvx's user avatar
  • 148
-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> - ...
Swudu Susuwu's user avatar
14 votes
1 answer
642 views

How is bash autocompletion for Inkscape tied to dbus and how can I stop it?

Recently i noticed that when i try to "tab-tab" complete a filename for some gtk applications (i do not use gnome) i get dbus errors. for example: $ inkscape pu (tab-tab, to complete the ...
gcb's user avatar
  • 953
12 votes
2 answers
999 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 &...
Ashark's user avatar
  • 1,209
4 votes
0 answers
229 views

Confused about SIGINT trap

I am confused about the processing of SIGINT and EXIT traps in Bash scripts. I wrote this script to test: #!/usr/bin/env bash set -e exit_trap() { printf "In exit_trap \$?=$?\n" exit 0 ...
Alexey's user avatar
  • 2,380
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 ...
luna's user avatar
  • 25
0 votes
0 answers
37 views

Is there anything like `tail -f` but that can follow multiple files and show which file each line came from? [duplicate]

I'm trying to monitor multiple log files simultaneously, but when I use: tail -f /var/log/app1.log /var/log/app2.log I get mixed output and can't easily tell which file each line originated from. What ...
Alphin Thomas's user avatar
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?
Florian's user avatar
2 votes
2 answers
321 views

How to add a prefix to all functions defined in a sourced shell script

I have a Bash script my.sh with commands such as: jup () { conda activate cd jupyter notebook } I source this file in .bash_profile with source my.sh and then I can run the command with ...
ginjaemocoes's user avatar
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 + ...
sdbbs's user avatar
  • 590
2 votes
3 answers
571 views

append iterator value to variable in for loop

I have a bash variable as such HAP="$(echo ${d} | cut -f 7 -d '/' | sed 's/[A-Z,0-9]\+//' | sed 's/_//')" that when is run as part of a script return either ref or hap. Now, I have another ...
Matteo's user avatar
  • 283
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 ...
Evan Fishbein's user avatar
0 votes
2 answers
134 views

How to prevent side effects when using an alias (e.g. cat aliased to batcat)?

I want to alias cat to batcat. Yet I do not want to break any script on my system that may depend on cat. How to ensure that it won't break existing scripts? I am on an Ubuntu machine on a bash ...
k0pernikus's user avatar
  • 16.7k
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 ...
Abhilash's user avatar
  • 669
2 votes
1 answer
155 views

Bash exception handling with a stack trace and cleanup code - how to implement?

When using the set -e flag in Bash, then it's difficult to get user-friendly error messages (a stack trace) and it's difficult to add cleanup code that executes before your script exits with an error ...
user3207874's user avatar
0 votes
2 answers
69 views

How to start vipw/vigr without my vimrc?

my vimrc seems to not play along with vipw/vigr (probably because i only allow root to place tmp/buffers/etc in a secure location) so I tried (using bash): user@host$ EDITOR='/usr/bin/vim -u NONE' ...
gcb's user avatar
  • 953

1
2 3 4 5
546