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.
26,588
questions
0
votes
2
answers
1k
views
SFTP with redirection and here doc has issues under Cron
The script below works fine when running from command line. I also put in a Cron Job, and I know the Cron Job is running because I see entries in the sftpFileLog.txt.
But when running under Cron Job,...
0
votes
2
answers
85
views
Append groups of images based on their total sizes
I have hundreds of images named in this pattern:
file-001.gif
file-002.gif
file-003.gif
...
Using, magick, I want to append every group of images with the condition that they don't exceed 950kb in ...
0
votes
1
answer
74
views
How to store and expand the variable in a command?
I am trying to get the size of a S3 Folder and store it in a INFLUXDB
Below is my code
#!/bin/bash -x
FILESIZE=$(mktemp)
s3cmd du -r "s3://path1/path2/" | awk '{print $1}' >> "$FILESIZE"
cat "...
0
votes
1
answer
27
views
Why none direcotry send via pipe to a grep get searched result?
My current directory:
pwd
/home/debian
Search a string -dig in /home/debian/mydoc/source.
grep -lr -e "-dig" /home/debian/mydoc/source
#get nothing as output
In my opinion ,sent nothing via pipe ...
3
votes
1
answer
110
views
extglob behaves different in bash of Fedora and Debian
Fedora 31 and Debian 10 behaves different when I run the following command:
/bin/ls /bin/!(znew) | /bin/grep znew
Fedora it outputs nothing, but Debian it does:
znew
znew exists on Fedora, if I ...
1
vote
2
answers
974
views
Running tmux sessions inside tilix terminal emulator
Background: I use Tilix as my main terminal emulator. In the Tilix preferences (attached screenshot), I have set it up so that when Tilix is launched, it will run this command:
/usr/bin/zsh -c "if [[ ...
1
vote
1
answer
462
views
Installation scripts doesn't work with sudo
I'm a Fedora 32, but I'm a Linux user for a long time, and this is the first time I saw something like this. Every time I try to use any install script, from any program, I have several bash errors ...
0
votes
2
answers
5k
views
tr and rot13 question
I'm playing Bandit by OverTheWire. On stage 11 they ask you to translate a data file with this plaintext "Gur cnffjbeq vf 5Gr8L4qetPEsPk8htqjhRK8XSP6x2RHh". I know that the solution is to use this ...
0
votes
0
answers
18
views
Alias or Function for a Command [duplicate]
Sometimes (or may be all the time) an Alias and Function for a command works the same way. For example, following Alias and Function of Ping Command:
alias Pingoogle='ping -c 3 google.com && ...
3
votes
2
answers
2k
views
what is the purpose of "cd ." command?
We know that the cd command is used to change directories. For example
cd dirname
valid dirnames include . and .. as well. The purpose of cd .. is clear. From the manpage:
A null directory name ...
2
votes
2
answers
871
views
Reverse characters in alphabet
How can I reverse
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
so that it would be something like
"ZYXW..."
I know it's a fairly basic question, but the help would be appreciated :)
0
votes
3
answers
138
views
Check If Any Directories In a List File Are a Subdirectory of a Specific Directory
I think I already know the answer to this and the answer is to "do the work" (i.e. loop over the individual entries in the file), but I want to see if anyone has any Bash magic.
Situation:
Let's say ...
0
votes
0
answers
99
views
using sed within a while loop
I am trying to use sed within the script to replace all , with spaces for the output of MyGROUP
MYGROUP=
for group in ${MYGROUP/,/ /} ; do
case $group in
cars) MY_GROUPS="$MY_GROUPS,red" ...
0
votes
1
answer
582
views
Gnome Shell resource extraction
I'm trying to extract gnome-shell resource using gresource extract using this script:
#!/bin/bash
declare -a n;
n=$(gresource list gnome-shell-theme.gresource | cat | tr '\n' ' ')
for r in ${n[@]}
do
...
1
vote
2
answers
20k
views
Parse curl response with sed
I'm trying to call a JSON API with the following curl command on macOS:
curl https://api.ipify.org?format=json
It returns something like this:
{"ip":"xxx.xxx.xxx.xxx"}
I would like to extract the ...
0
votes
0
answers
133
views
How to hide contents of a named pipe?
I have a writer process and a reader process which communicate using a named pipe
/FIFO/pipeAB which is created with permission 666.
The contents of the pipe can be viewed from bash shell at runtime ...
3
votes
1
answer
1k
views
How to redirect this "Floating point exception(core dumped)" error?
I want to redirect the output of a program to a file with variable name and, at the same time, redirect the possible error. This is the script so far:
#!/bin/bash
echo "Se ejecutará el PET-linux.x ...
0
votes
1
answer
486
views
Trouble piping output of command into zathura
The zathura man page states
zathura displays the given files. If a single hyphen-minus (-) is given as file name, the content will be read from standard input. If no files are given, an empty ...
1
vote
1
answer
3k
views
Why is exit code 0 even though the command is wrong?
I am testing different things in bash scripting and I came into a problem that I cannot get an explanation for it.
I am purposely writing a wrong command, to get an exit code different from 0, but ...
0
votes
1
answer
858
views
if variable = then set variable to BASH
How can I do something like this
if [[ $variable = name ]]
then
$variable=fname
fi
if [[ $variable = surname]]
then
$variable=sname
fi
so change the value based on on result
...
2
votes
1
answer
2k
views
Getting directory name from file without full path
I am trying to get the directory name of this file, which is not located in my current directory:
/home/me/PhD/file.tsv
when I run:
dirname /home/me/PhD/file.tsv
I get:
/home/me/PhD/
But, I ...
2
votes
1
answer
234
views
Is there a command that lets me quickly close and reopen multiple files in different locations, similar to how pushd popd work?
I want to be able to edit several files quickly. It would be great if there is a tool that allows me to stack files similar to how pushd and popd does. Does such a tool exist? If not, what do you ...
0
votes
1
answer
37
views
Proper terms for symbols
What are the proper names of following symbols/operators in Linux?
>
>>
<
4
votes
1
answer
2k
views
Bash and sh scripts reading Here strings differently
I have this line in my code:
output=$(sed 's/\/$//g' <<< $output)
It works in #!/bin/bash but not in #!/bin/sh. In sh program exits with Syntax error: redirection unexpected, and after ...
1
vote
1
answer
898
views
Use line break in Linux command
I'm trying to run a command in a command line (just copy and paste the into moba xterm to remote Ubuntu machine). The command contains a line break, so I decided to use $(command). Here is the command ...
1
vote
1
answer
216
views
Differences between (command) and command in bash
While investigating the problem of redirecting the stderr of the bash keyword time and the standard solution to the problem, I came across another question.
The description man bash gives of the (list)...
1
vote
2
answers
68
views
Is it possible to start Bash up with better single quote characters?
In this answer, user pizdelect explains how to start a new Bash subshell that uses U+2018 and U+2019 for the single quote characters, but notes that you can't set this from inside bash. Therefore it ...
1
vote
0
answers
58
views
what should echo [:space:] print? [duplicate]
What should commands echo [:space:] and echo [:alpha:] print on bash terminal? The behavior seems pretty random.
Sometimes it just prints the whole strings [:space:] or [:alpha:], at other times ...
1
vote
1
answer
99
views
My multiple variables in a function do not get expressed as aliases. I think my syntax is faulty
In bashrc, I have this alarm function which can take 3 variables:
a () {
local $1="${1:-3600}"
local $2="${2:-paa}"
local $3="${3:-alarm}"
sleep "$1" && $2 && $3
}
alias ...
0
votes
2
answers
1k
views
Expand (certain) aliases from within sudo bash function
There are two things I want: 1) I want that if I keep using sudo often that it never asks me for my password by updating my cached credentials every time I type sudo, 2) That I can use (certain) ...
0
votes
1
answer
152
views
How to measure total cumulative time spent in sleep()?
I am analyzing a tool written in bash and python. The call from the top traverses through many layers of bash and python programs. The time to completion is in the order of hours (4-5 hours). In these ...
0
votes
2
answers
1k
views
Find all the shell used among all the duplicate users in etc/passwd file
I have tried to find the duplicate username in /etc/passwd file in the following way:
#!/bin/bash
# passwd file are look like below
# username : password : user ID : group ID : user ID info : home ...
0
votes
2
answers
163
views
Need help understanding shell command with pipes, redirections and remote connections
I have some experience using the bash, but this command here which I saw in a tutorial caught me off guard:
cat ~/.ssh/id_rsa.pub | ssh git@remote-server "mkdir -p ~/.ssh && cat >> ~/....
1
vote
2
answers
1k
views
How to escape a "\" in command parameters [duplicate]
I have a command
path/to/forticlientsslvpn_cli --server <host>:<port> --vpnuser testpass\101
When I run the script, linux puts a space between testpass and 101. I want the script to see ...
1
vote
0
answers
43
views
"bash: _variables: command not found" with autocomplete
I'm using Debian GNU/Linux 9.12 ( Kernel: Linux 4.9.0-12-amd64 ), and I'm having problems with autocomplete. Autocomplete does work but it shows the message
bash: _variables: command not found
...
1
vote
1
answer
3k
views
Passing a variable from one function to another in bash script
Iam new to linux and trying to pass a variable from one function to another in a same bash script.
Below is my code:
#!/bin/bash -x
FILES=$(mktemp)
FILESIZE=$(mktemp)
command_to_get_files(){
aws ...
0
votes
2
answers
334
views
execlp() system call
I want to call the ulimit shell command through a C program. right now, I have:
execlp("ulimit", "ulimit", "-u", "%d", ulimit_a);
It compiles fine, but does not actually edit the user-id max ...
4
votes
1
answer
163
views
Why do shells use a backtick for an opening quote in much of their output?
$ type -a short_command
short_command is aliased to `some_long_and_obnoxious_command_that_I_never_want_to_type'
Why does Bash (and perhaps every other shell) frequently—as above—use a ...
1
vote
1
answer
1k
views
What happens to arguments passed to bash/ash functions?
This answer about -- gives a lot of good information about using -- in POSIX shells and this answer about passing all arguments to a script led to the following function:
my_echo()
{
set -x
...
2
votes
4
answers
4k
views
Sum all positive integers in a bash array
How can I sum all positive integers in a bash array.
Here is my code:
#!/bin/bash
arr=(2 43 -1 -33 24 12 -6)
for (( i = 0; i < ${#arr[@]}; ++i )); do
if (( arr[i] > 0 )); then
sum=`...
1
vote
3
answers
84
views
How can I grep columns that match in the same row?
I want to get the end date for an installed subscription, and currently using
subscription-manager list --installed | grep Ends | awk '{print $2 }')
The complexity comes when there are multiple ...
0
votes
3
answers
426
views
Does the Linux terminal have the ability to auto-complete for files deep in directory structure?
Is there any way in a Linux terminal to refer to a file in a very deep directory structure without specifying the exactly path?
For example, if you know you have a file called qstr.js somewhere 3-4 ...
1
vote
1
answer
206
views
Bash Script: Loop through a file
I have a file with some lines in it:
/snapshot/200
/snapshot/201
/snapshot/202
Now i'm trying to use each line with a while IFS loop to do certain actions with them.
while IFS=' ' read -r test || [ ...
0
votes
2
answers
218
views
How to expand variable within a single-quoted argument?
I am trying to perform the following without luck:
SORT_BY='-k3,3r -k2,2 -k1,1r'
awk 'NR<4{print $0;next}{print $0 | sort '"${SORT_BY}"' -t"~"}'
I have tried with all sorts of quotes, unquote, ...
0
votes
1
answer
35
views
Write changes to script after adding array element
So I have the following script which will show me my bookmarks in dmenu then open the selected Item in firefox.
#!/bin/sh
declare -a bookmarks
bookmarks=(
https://www.reddit.com/
https://www....
2
votes
2
answers
1k
views
How can I expand and edit the result of a command substitution in Bash?
Is it possible to expand and edit the result of a command substitution $(<cmd>) in Bash?
Suppose that <cmd> results in another command that I want to execute. Indeed, $(<cmd>) will ...
0
votes
1
answer
72
views
How to partially match only one instance of a filename?
So i have a list of files, that i renamed to filename:hash.
What i wanted to do is to only match the hash, while keeping the combo filename:hash intact, and not calculating their hash again, since ...
1
vote
4
answers
9k
views
How to pass multiple variable to a for loop as a argument in a bash script? [closed]
I am new to linux and i am writing a bash script...
In the script, I have 2 variables(inside the varibles contents are there). I am trying to pass these 2 varibales in a same for loop and perform some ...
7
votes
3
answers
4k
views
How do I create a loop of variable length to print random numbers?
I'm trying to create a matrix of random numbers after prompting the user for the number of rows and columns of random numbers. I can only use nested for loops.
I have created two variables: rows and ...
0
votes
1
answer
1k
views
How to convert curl response xml to plain/csv/xls for a total count
I have a script that send sms for a trigger using a sms gateway. The command to send sms used is curl as below:
curl 'http://www.example.com/push/server.php?user=******&pass=*******&sid=******...