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
12 votes
4 answers
2k views

Is there a shortcut in bash to delete the last segment of a path?

I've become pretty proficient with a number of bash shortcut keys that make my bash-ing faster: C-a/C-e, C-u, C-w, M-f/M-b, C-r etc. One common task that I haven't found a good shortcut for though is ...
1 vote
3 answers
927 views

Customize the way stderr is appended to a file

I have an issue with a shell script that uses rsync to synchronize 2 directories. I'm storing the result of the rsync command (whether it's successful or not) to a file named status and in case there ...
  • 179
13 votes
1 answer
4k views

Remap bash vi keys?

I use Dvorak and vi, so when I found out about the vi mode in Bash, I was very excited. However, it would be even better if I could remap the movement keys to be the ones that I use in vi (I swap keys ...
  • 233
6 votes
1 answer
6k views

In Bash, why is PROMPT_COMMAND set to something invisible?

On RHEL6 and CentOS 6, /etc/bashrc sets PROMPT_COMMAND here: case $TERM in xterm*) if [ -e /etc/sysconfig/bash-prompt-xterm ]; then PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm ...
3 votes
2 answers
112 views

How to output only given chars?

How can I crop unneeded chars, and only output what I need? For example: input: ASDFQWER output: DFQ I mean in this case I need only the 3.-4.-5. chars of the string. How do I do that?
  • 38.4k
10 votes
2 answers
2k views

How to do block selection in `screen`

In vim, there's a CtrlV to do block selection. How can I do it in screen?
  • 911
14 votes
3 answers
15k views

How to setup trap in bash functions?

I can setup trap in bash scripts but not bash functions. For example, source the code below and run f, now try Ctrl+C, it's not been trapped! How to setup trap in bash functions? 4 trap ctrl_c ...
  • 6,421
5 votes
1 answer
335 views

Why is the following command killing a system?

Anyone understand the following code , running in bash ? :(){ :|:& };: It seems to be a "fork" bomb on Linux.
  • 52.8k
2 votes
1 answer
1k views

Tab and complete

Is the key binding for tab to complete in the readline settings set implicitly. Because when I outputted bind -q complete I got this in return complete can be invoked via "\C-i", "\e\e". Thanks ...
  • 1,667
2 votes
0 answers
1k views

What happens when you background a function which calls another function?

I'm trying to find out how to background a process in a script that calls another function and reliably terminate it when ready. Will I have to move it all into one function? function dosomething { ...
  • 141
1 vote
1 answer
991 views

How to demux audio and know what type it is?

How to demuxed audio and know what type it is? When muxing an audio into a video, ffmpeg, mencoder, etc require to know what the audio's encoding is. The encoder typically uses the file name's ...
  • 32k
16 votes
2 answers
3k views

Setting readline variables in the shell

I read in the man page, that you set readline parameters on an off or to a value by using set var value Is this the same as the set builtin, and how do you set the variables once inputrc has already ...
  • 1,667
11 votes
3 answers
27k views

How to discover a new line using a for loop?

In various places around the web I've found: \015 \012 \x0a - hex \n \r all as synonyms for various newlines / carriage returns... But in this small script I cannot recognise when I come across a ...
  • 141
3 votes
4 answers
14k views

Bash options and interactive shells

The -s option to bash doesn't seem to really do anything When I execute commands with or without it nothing changes. What really confuses me is when I execute a shell script I always seem to be ...
  • 1,667
1 vote
2 answers
387 views

conditional expression and expansion

I don't seem to understand how to "test" the conditional operators [[ and [. I tried using various form such as echo [[a=a]] [[a=a]] echo $? and some other things I want to see what they return, ...
  • 1,667
35 votes
8 answers
52k views

How do I reverse a for loop?

How do I properly do a for loop in reverse order? for f in /var/logs/foo*.log; do bar "$f" done I need a solution that doesn't break for funky characters in the file names.
  • 2,983
2 votes
2 answers
1k views

Why does "$i | sed" not work?

I have this code: if [ $i | sed -e "s/^.*\(.\)$/\1/" = "/" ] then echo "folder" else echo "file" fi where $i is something like this app4/ The code above should get the last char of the ...
-2 votes
1 answer
369 views

Why does this script show all files in the directory and not just PDF files?

I wrote the script below to find the number of PDF files in a given directory. However, it instead shows all the files in the directory: #!bin/bash message="." message1="*.pdf" ls -al $message $...
5 votes
3 answers
5k views

Is it a script or is it a binary?

At the end, it looks like it's just a regular sh script: [USER@MACHINE ~/Desktop/D] head -2 truecrypt-7.1-setup-x86 #!/bin/sh CHECKSUM=163123652 but @the scripts end: [USER@MACHINE ~/Desktop/D] ...
  • 38.4k
2 votes
2 answers
7k views

Is the `\` character escaping or becoming part of my ssh key password

If I do: ssh-keygen -N password123\$ -f bobskeys Is \ escaping the $ character or becoming part of the password? Or rather, will bash be doing any escaping before ssh-keygen gets the password ...
  • 1,419
7 votes
2 answers
8k views

Is it possible to get the error message from previous command which failed when the conditional command runs using ||

I have a helper function: function error_exit { /opt/aws/bin/cfn-signal ... exit 1 } This helper function is used to signal an error. Here is an example of usage: /opt/aws/bin/cfn-init -s .....
  • 307
4 votes
1 answer
1k views

wait for autossh connection to complete

I'm trying to create a script that runs a few commands that take a long time to execute and require a constant connection, but autossh user@server wait $! #or wait ${!} commandA commandB doesn't ...
  • 143
0 votes
1 answer
3k views

-bash: command: command not found [closed]

I'm trying to run a program from the command line, and I got a -bash: command: command not found error. What can I do to troubleshoot/fix the problem?
  • 1,811
133 votes
11 answers
350k views

Script to change current directory (cd, pwd)

I want to run a script to simply change the current working directory: #!/bin/bash cd web/www/project But, after I run it, the current pwd remains unchanged! How can I do that?
  • 1,433
2 votes
0 answers
117 views

How do I get this Bash-script to work? [duplicate]

Possible Duplicate: How to name a file in the deepest level of a directory tree How do I get this script to work? The goal is to find the directory at the deepest level in the directory tree and ...
2 votes
2 answers
3k views

su - username -c "./script.sh" -> no such file or directory

We have: a user nico. a folder /home/nico/script_home with some bash scripts: main.sh subscripta.sh sqlscripta.sh We connect as root and cd /home/nico/script_home/ Finally we run ./main.sh We get ...
103 votes
8 answers
364k views

#!/bin/bash - no such file or directory

I've created a bash script but when I try to execute it, I get #!/bin/bash no such file or directory I need to run the command: bash script.sh for it to work. How can I fix this?
6 votes
1 answer
2k views

"Safe mode" on Linux?

Under RouterOS/Cisco there is a function called "safe mode". When someone presses CTRL+X then the terminal goes in "safe-mode" that meant that if the terminal exists in a "bad way", ex.: the user ...
  • 38.4k
1 vote
1 answer
4k views

Script to SCP files works manually but not through cron

I'm using CentOS 5.6. The below code might have some typos as I needed to change real info to fake info for security reasons: backup.sh #!/bin/bash set -vx rm -v /server/temp_db.gz rm -v /server/...
  • 11
4 votes
8 answers
6k views

How to name a file in the deepest level of a directory tree

How do I name one random file in the deepest level of a directory tree using basic Bash commands and script? I was searching for a way to do it as an one-liner and without functions. Assuming also ...
55 votes
10 answers
64k views

Displaying seconds as days/hours/mins/seconds?

Is it possible to easily format seconds as a human-readable time in bash? I don't want to format it as a date, but as the number of days/hours/minutes, etc...
  • 5,771
5 votes
5 answers
1k views

evaluate multiple patterns from program output and write into pattern specific files

I have a script outputting some value/numbers and I want to split those into two files. I am looking at something like: ./runme.sh | grep 'ook' >> ook.out | grep 'eek' >> eek.out Where ...
3 votes
1 answer
158 views

Calling a script on each line of a file

I have a bash script that takes 3 arguments: $ do_something foo bar baz and a file with several lines, each with different 3 args. I want to execute the script as a cronjob, and each time it's ...
  • 1,809
1 vote
2 answers
3k views

How to fix this error? make: *** [obj/local/armeabi/libgstaudioflinger.so] Error 1

I am trying to install this following framework from my Linux Fedora box to Android using there SDK/NDK. But after following all instruction its getting still failed to get build. Any idea how to fix ...
  • 3
1 vote
1 answer
2k views

grep searchstring /etc/crontab strange behavior in BASH script

I am working on a BASH script which creates automated backups via the crontab. I am trying to use myVARIABLE=`grep backup /etc/crontab` to return lines containing "backup" if an entry has already ...
0 votes
1 answer
2k views

POSIX and Bash, sh shorthand, for compound if statement

I prefer to do my sh commands in shorthand, And I prefer to be in POSIX. Truthfully, if the script can run on Dash, that is good enough. Which means using the test command [, not the extended [[. I ...
  • 4,741
24 votes
6 answers
13k views

Abbreviated current directory in shell prompt?

vim has a really nice feature which it utilizes in its paths when they're a bit long: It abbreviates the path to the document in the tab at the top. Is there a way to do something similar to this so ...
  • 37.8k
3 votes
1 answer
2k views

Redirecting output when I execute "bash -c ..." from a C program

Say I have a C program and in it I want to execute a shell command and redirect its output to file, by using the stdlib system function. system("bash -c \"echo $HOME\" > a.txt"); Now this is for ...
  • 5,610
24 votes
17 answers
9k views

"Bookmarks" for bash

Navigating several layers of nested directories is often a pain. On Firefox, it is easy because they have bookmarks. So what I'd like to do to bookmarks a file is to type: $ go --add classes "repo/...
  • 1,031
16 votes
2 answers
5k views

Readable comments on separate lines in a multi-line bash command with pipelines?

When creating shell scripts using pipelines, and using the backslash to continue lines, I want to insert comments on separate lines, in a robust, readable and portable fashion. For example, given ...
2 votes
1 answer
11k views

How to set a new password from bash?

I'm using busybox with a limited passwd (I don't have --stdin option) and without chpasswd and I need to change the password of an user from bash. Here is my best result: echo newpassword > ...
9 votes
2 answers
7k views

autocomplete filenames using compgen

As part of a larger autocomplete function I'm writing, I want to use compgen to generate a list of files. I read the bash manual entries for compgen and complete, and from there I assumed that the ...
85 votes
5 answers
208k views

Refresh env variables after editing bashrc file

I frequently edited the .bashrc file to export new environment variables. Rather than close the console and start a new one to refresh the env variables, is there a convenient way to refresh?
  • 1,625
25 votes
1 answer
33k views

How do I set a user's default .bashrc file?

On Ubuntu 10.4 I have edited the /etc/bash.bashrc file to set some variables like the command history size (HISTSIZE=5000), however if I create a new users Ubuntu by default gives them a .bashrc file ...
  • 43.5k
36 votes
5 answers
117k views

How to read from two input files using while loop

I wanted to know if there is any way of reading from two input files in a nested while loop one line at a time. For example, lets say I have two files FileA and FileB. FileA: [jaypal:~/Temp] cat ...
  • 1,582
27 votes
4 answers
21k views

How to hide shell process information when starting background process?

[USER@SERVER ~] sleep 3 & [1] 5232 [USER@SERVER ~] [1]+ Done sleep 3 [USER@SERVER ~] How do I /dev/null these two messages?: [1] 5232 [1]+ Done sleep 3 ...
  • 38.4k
12 votes
3 answers
2k views

Send task to background in an "if"

Why is this? if true; then sleep 3 &; fi bash: syntax error near unexpected token `;' I want to run sleep 3 in the background so that the command ["sleep 3" is just an example] would run in "...
  • 38.4k
5 votes
2 answers
2k views

How can I copy all users .bash_history files into my home directory?

I am running as root. I want to copy all the users on the system's .bash_history files into my home directory. I can do this to combine all of them into one, but then I can't tell who's commands are ...
  • 43.5k
3 votes
1 answer
4k views

Why my $PATH is not inherited by the invoked bash?

I work on an AIX system where I have no administrator privileges. It has several shells installed, default being tcsh. I am not allowed to change the login shell. Usually I start my session from exec ...
17 votes
5 answers
13k views

Colored FIND output?

Is it possible to get colored output from the find command? That is, in the path of each found item, directories are blue, executable scripts are green, et cetera? I'm using version 4.4.2 of GNU ...
  • 315

1
498 499
500
501 502
513