Skip to main content

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.

Filter by
Sorted by
Tagged with
0 votes
1 answer
25 views

How to modify this git alias so it can be run without the need to put multiword commands in quotation?

I have this alias: git config --global alias.all '!f() { ls -R -d */.git | xargs -I{} bash -c "echo {} && git -C {}/../ $1"; }; f' It currently works fine when I run it like : git ...
Codrguy's user avatar
  • 123
0 votes
2 answers
55 views

Regarding Nested loops

I had 3 array array1, array2, array3 where array 3 is the value of first two array. for example: FO=2.0, FF=3.5, FC=4.5 and so on. I want to give k (third loop) values of array3, like while running FO ...
Khushi's user avatar
  • 1
0 votes
0 answers
14 views

macos/bash: read "hangs" when executing script second time as another user with stdout redirected before

Hello! My second file descriptors question. Context: Still new at this, I certainly don't get file descriptors. Magic. Unpredictable magic at that, that sometimes works. (joke, but you probably get my ...
Marcus Widerberg's user avatar
0 votes
0 answers
17 views

Looping ssh command with `while` stopped at first iteration but not `for` [duplicate]

The following didn't work: cat /etc/hosts | grep -vE '(^(#|$)|127.0.0.1|ff|::|node)' | awk '{print $1}' | while read -r host; do echo "Looking at $host" ssh "$host" ...
annahri's user avatar
  • 2,075
1 vote
0 answers
18 views

While Loop Exits After Calling Function [duplicate]

I'm trying to do a db backup script using Bash. When adding a "Resume" feature to the script, I do the following loop calling defined functions to do step by step (depending on the .lock ...
James's user avatar
  • 111
0 votes
1 answer
38 views

basename complains about missing operand using unshare

If I try to create namespaces, basename complains about missing operand: sudo unshare --mount --ipc --uts --pid --fork --user /bin/bash basename: missing operand Try 'basename --help' for more ...
sebelk's user avatar
  • 4,469
-2 votes
1 answer
59 views

How to convert multiple columns to rows in shell (unpivoting)

I have a tab separated table with hundreds of columns and rows, like that: OTUId AC0184 AC0250 AC0257 AC0258 AC0262 AC0263 AC0289 AC0293 OTU549024624 51659 0 0 0 0 0 0 0 OTU565712448 108 0 0 0 0 0 0 0 ...
babinecha's user avatar
-1 votes
1 answer
55 views

Why doesn't 'export' give the export attribute while 'set -a' does?

From man bash (GNU bash, version 5.2.21(1)-release (x86_64-pc-cygwin)): set [-abefhkmnptuvxBCEHPT] [-o option-name] [--] [-] [arg ...] -a Each variable or function that is created or modified is ...
pmor's user avatar
  • 619
0 votes
1 answer
44 views

How can I get stderr from mbuffer written to log file when using tee?

Having a bash script that essential only doing: mbuffer --md5 -i dummy.tar -o /dev/null A user of this script wants to use tee and store the output messages in a log file. ./script.sh 2>&1 | ...
TopCatSE's user avatar
2 votes
4 answers
330 views

output of a command framed by a couple of '#' characters

I want to be greeted by # ##################################################### # ### PostgreSQL 16.3 ################################# # ##################################################### on ...
vrms's user avatar
  • 161
0 votes
1 answer
36 views

tar returning different exit code when wrapped inside if statement [duplicate]

I have a script which creates a tar archive filtered through xz. I have wrapped the tar command inside an if statement so the script can take different actions depending on whether the archive was ...
mongotron's user avatar
1 vote
2 answers
74 views

How to append multi-line text with spaces to end of file if that text does not exist in bash?

I'm trying to append a text string that contains newlines and spaces to a file if the whole string doesn't already exist in that file. Ideally done in a way that's portable across MacOS and Linux I ...
David Alsh's user avatar
0 votes
0 answers
17 views

How do I determine if a path is a fifo? [duplicate]

I have some bash shell code that runs if [[ ! -s "$1" ]] to determine if $1 is a non-empty file This test fails if $1 is a fifo, even though the fifo has data in it ready to be read. Is ...
Greg Dougherty's user avatar
-1 votes
3 answers
90 views

Delete 2-line paragraphs, keep 3-line paragraphs, maintain newline between all

On a Linux machine with GNU tools I have an ascii file that consists of only 2-line and 3-line paragraphs, each paragraph separated by a newline. All 2-line paragraphs are to be deleted so that all 3-...
stampeder's user avatar
3 votes
1 answer
40 views

Is it normal for "complete -p git" to error until I autocomplete git first?

If I restart my terminal, I get an error when I type this: $ complete -p git bash: complete: git: no completion specification But if I type git tab then run the complete command again, I get a ...
Daniel Kaplan's user avatar
1 vote
2 answers
209 views

How to make ssh return immediately after running background job

When I run on linux-system/bash: sleep 10 & I get bash prompt immediately. But when I run: ssh linux-system 'sleep 10 &' I wait 10 seconds. Two questions: Can I find explanation in ssh/bash ...
Philippe's user avatar
  • 1,475
5 votes
2 answers
488 views

Iterating over the contents of a file

I understand that the output of file.txt is treated as input for the next command in the pipe. Yet, I don't understand how while in conjuction with read traverses the file from one line to the next? ...
0binny0's user avatar
  • 59
-2 votes
1 answer
99 views

Linux Bash Script - Yes or No - read answer

little script #!/bin/sh cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 24 | head -n 4 printf 'do you like this? (y/n)? ' read answer if [ "$answer" != "${answer#[Yy]}" ] ;then ...
user447274's user avatar
0 votes
0 answers
41 views

bash completion stopped working out of the blue [closed]

Running Debian unstable here. Two days ago, tab completions in bash stopped working in Konsole. No recent upgrades (except any security upgrades that may have happened in the background). I checked ...
user2751530's user avatar
2 votes
1 answer
160 views

Modify a key value with another one containing a path using slashes

I have a couple of hours (I started today) experience in Bash. I have been trying multiple times to modify the value from a key in a json file using it. I know that it's much easier in python but I ...
Diego Alejandro Gómez Pardo's user avatar
2 votes
1 answer
640 views

Which interpreter for "Unicode text, UTF-8 text executable"

I'm trying to set up a keybinding for an executable which is in my home. For this, I set the command: sh -c '\"/path/to/the/executable\" --options' But, it does not work, and, when I'm ...
Phantom's user avatar
  • 453
2 votes
1 answer
41 views

Shell/bash: Can I create a file descriptor to an existing file without emptying the file? [duplicate]

Context: I have cursory bash experience. I do not fully get file descriptors, just some basic usage. Trying to create a setup script. Most done, but a few "kinks" remain. So here is a newbie ...
Marcus Widerberg's user avatar
0 votes
2 answers
115 views

How to execute command if previous command fails with return code 0

I have a script which logs in to a server. Regardless of the success (i.e. whether the password is valid or not) the script will exit with return code 0, and the text: Error login failed. I'd like it ...
Nickotine's user avatar
  • 489
1 vote
2 answers
60 views

Use sed to insert a character in an xml file

I have an xml file with content following this format: <type:color>000000</type:color> <type:color>FFFFFF</type:color> The information within the tags are hex colors (many ...
jon danniken's user avatar
0 votes
3 answers
34 views

Play sound after command and also pipe output

I can easily pipe the output of a command like: some_command | less I can also easily play a sound after a command like: echo test; print \\a aplay, beep or others could also be used for sound. ...
Luis's user avatar
  • 101
0 votes
1 answer
73 views

Why is head -c (and similar commands) much slower for large files than small files?

From other things I've read (for one example of many why does it take so long to read the top few lines of my file?_ ) I gather that commands like head -c 4 bigfile dd if=bigfile bs=4 count=1 or c++ ...
Jack's user avatar
  • 101
1 vote
1 answer
81 views

Bash completion after quotation mark

I am experimenting with bash completion to write a general completion library in C associated with command line parsing and stumbled in a behavior for which I can find no documentation. I am using the ...
user1542207's user avatar
1 vote
3 answers
37 views

Bash: Access variable with another variable [duplicate]

How can I access a variable with another variable, like this? varAble="Hello" varA="\n" varBable="World" varB="END" for part in A, B do echo var${part}ble ...
binaryBigInt's user avatar
0 votes
2 answers
22 views

How do I update the contents of a script including comments (django/settings.py) using echo, printf or grep

I would like to overwrite a Django/app/settings.py text file using the Linux terminal command printf. The settings.py starts with docstrings comments, i.e. """some text"""...
Tommy Gibbons's user avatar
-1 votes
2 answers
92 views

runuser: execute commands without use of quotation marks?

Overview I'm attempting to execute commands as a different user without using quotation marks as well as making sure the executed command isn't inheriting environment from the parent shell. runuser ...
GrabbenD's user avatar
  • 146
0 votes
2 answers
68 views

variable with hyphen in bash script

db1="community-service-data" db2="ecomm-data-test" drop_db="DROP DATABASE ${db1}" sudo -u postgres -S /bin/bash -c "psql -U postgres -d postgres -c \"${drop_db}...
CodeWeed's user avatar
  • 137
0 votes
2 answers
51 views

Bash script to compress nested directories not traversing correctly

I have a folder structure that looks like this: 0/1/2 and the numbers goes from {0..9} I tried writing a bash shell script so that I can compress each of the folders like this: #!/bin/bash for i in {0....
alpha754293's user avatar
1 vote
2 answers
80 views

Command not found on shell opening

Every time I open the shell, the first line on the terminal is "bash: n: command not found". I tried to look at /etc/profile, ~/.bashrc, ~/.profile and ~/.bash_profile but I was not able to ...
Alessandro's user avatar
0 votes
2 answers
79 views

How to write an alias or bash script that renames a single file using the same syntax as the `ren` command of Windows?

I am a long-term a Windows user, so I find the syntax of the ren command both simple and convenient: ren C:\pathTo\myFile\oldFileName.txt newFileName.txt I want to be able to use the same syntax on ...
ps.pf's user avatar
  • 3
-1 votes
1 answer
66 views

when is .bashrc read?

I have been scratching my head trying to figure out why & how my ~/.bashrc file is read :/ I'm connecting to my Debian bookworm server through ssh. It's an interactive login shell. According to ...
Tchupy's user avatar
  • 11
0 votes
1 answer
25 views

view a bunch of text files in the terminal [duplicate]

I've got a bunch of text files and pdfs in a directory. If I enter evince *.pdf ..all the pdfs open up in separate windows, I do this quite often. When I enter cat *.txt ..was pleasantly surprised ...
cardamom's user avatar
  • 602
0 votes
0 answers
24 views

Evaluate command inside comparison in makefile [duplicate]

I would like to check if Linux system is ubuntu inside Makefile. What I do is: copy_files: if [ "$(head -n 1 /etc/lsb-release | cut -d '=' -f2)" = "Ubuntu" ]; then \ ...
ellat's user avatar
  • 37
-1 votes
3 answers
75 views

Bash AND operator (&&) causes unexpected token if used within an if statement

I have found && to cause an error in bash when used within an if-statement. I am wondering if anyone has an explanation as to why this occurs and if there is a way to only run command3 if ...
Theis Jendal's user avatar
6 votes
1 answer
362 views

Why does redirecting stderr interfere with bash's handling of $COLUMNS and the `checkwinsize` option?

I'm scripting a testing framework in bash and am running into a weird situation. A couple things I'm doing in this post for illustration purposes: Adding echo lines to the scripts; these are not part ...
Targeloid's user avatar
  • 229
0 votes
2 answers
80 views

How can I properly harden my system against some basic exploits like modifications of .bashrc?

I recently read a section on bashrc in the ubuntuusers wiki, in which a possible attack vector for privilege escalation is pointed out. Attackers, or malicious programs, could modify the bashrc or ...
hefe's user avatar
  • 183
0 votes
0 answers
20 views

Setting variable (ORACLE_BASE) for oracle user | Installing Oracle 23

Goal: Install Oracle 23 into /u00/app (and not the default, /opt/oracle). Problem: Oracle installs into /opt/oracle (I assume the variable ORACLE_BASE etc. is not properly set / read for user oracle ...
DavidDunham's user avatar
0 votes
0 answers
27 views

tool or shortcut for copying part of the terminal

I apologize for the vagueness of this question but I saw this tool over 4 years ago at this point. I remember working with a colleague at one point who knew a bash tool or shortcut that allowed him to ...
modernNeo's user avatar
  • 185
2 votes
1 answer
91 views

What is the limit of string size passed to bash -c?

I have the following command echo '123456789#' | xargs -I% bash -c 'echo %%%%%%%%%%%%%%%%%%%%%%%%%' It doesn't expand the last %. Apparently there's a limit on how long the string passed to bash -c ...
gicig's user avatar
  • 121
-4 votes
3 answers
83 views

show which of a list of filenames do not exist in a given list of directories

I have a long list of filenames like 20231121_150111.jpg... in a text file (one filename per line). And another list of directories. Those directories contain files. How can I check and show which ...
user447274's user avatar
3 votes
4 answers
378 views

Remove duplicates in file (without sorting!) leaving the _last_ of the occurences

Following https://www.baeldung.com/linux/history-remove-avoid-duplicates, I added clean_history() { awk '!a[$0]++' $HOME/.bash_history > $HOME/.bash_history.tmp && mv $HOME/....
Make42's user avatar
  • 699
4 votes
1 answer
997 views

Are there alternatives to alias I'm not aware of?

Today I started to notice weird behavior running a local command for a binary installed through Go. It seems the command is aliased, but consulting alias, it isn't. The command buf somehow executes cp!...
Tim's user avatar
  • 1,054
0 votes
0 answers
25 views

Ghost string in terminal, string doesn't go away when using backspace [duplicate]

Linux shell problem on a server, bash 5.1.4(1)-release. Debian 11.10. I have this on my command line right now after pressing the up arrow: server# ctr plugins listkubeadm init --pod-network-cidr=10....
AttributedTensorField's user avatar
1 vote
2 answers
58 views

Need to make multiple multiline replacements in file

I have a file with multiple "paragraphs" like this: <type>TRANS</type> <attributes/> <specification_method>rep_name</specification_method> <trans_object_id/&...
Todd Porter's user avatar
0 votes
1 answer
73 views

How to put all function/command call output results to different corresponding vars: for stderr, for stdout, for string result and for return code?

I want to extend question How to store standard error in a variable and get general solution (bash function or functions) to extract from the called function/command together with standard error also ...
Anton Samokat's user avatar
-1 votes
2 answers
181 views

Is it possible to translate bash to another human language like Arabic?

Bash commands are written in the Latin alphabet, is it possible to translate them to another language? In this case, Arabic. Arabic is written rtl and I remember I tried it using alias before when I ...
Abdelaziz Baheg's user avatar

1
2 3 4 5
523