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.

Filter by
Sorted by
Tagged with
8
votes
7answers
15k views

Linux script or program to shorten filenames

I haven't had much success in finding a Linux version of namemangler, which I need to rename 1000 of files so they are readable on Windows. Does anyone know of a Linux program that can do this? If ...
9
votes
2answers
3k views

Is it safe to remove a script file from within that script?

Imagine I have a script foo. It should be run once when the user logs in and isn't needed after a successful run. My question: Is it safe to remove the script file from within the script? E.g.: #!/...
39
votes
10answers
48k views

How to check if a pipe is empty and run a command on the data if it isn't?

I have piped a line in bash script and want to check if the pipe has data, before feeding it to a program. Searching I found about test -t 0 but it doesn't work here. Always returns false. So how to ...
9
votes
3answers
17k views

How to split the output and store it in an array?

This is the output: 3,aac-lc, 93.8, aaclc, 77.3, h.264, 1024.6, h.264, 1029.1, 31, 31, 0,0,0.000000,31,31,0,0,0.000000,7,0,0,0.000000,30,1280 720,10,0,0,0.000000,30,1280 720 I tried with 2 scenario: ...
10
votes
4answers
6k views

Have Bash script wait for status message before continuing

I'm firing up Selenium server with a bash script and as you can see from the timestamps on the log below, it takes about 32 seconds for the thing to fully come online: Feb 28, 2012 10:19:02 PM org....
3
votes
1answer
6k views

Real time system info script using ncurses and dialog

I'm trying to create a simple bash script that I can run over ssh that will display some basic system information using ncurses and the dialog command. I've tried two approaches so far: 1) Run dialog ...
6
votes
1answer
3k views

Use sed to fix broken newlines

I have a folder full of files, each with many hundreds of lines. Many of these files have occasional broken newlines: when I open them in vim, many have ^M^L interspersed where newlines should be. ...
1
vote
2answers
584 views

need portable crontab host-checking/failover logic across Linux & Cygwin

A host-ping test that always works correctly from the interactive bash command line in Cygwin, is behaving incorrectly in its own crontab--always selecting the second host--and I can't figure out why: ...
4
votes
4answers
1k views

Converting a history to a script?

Often times I've typed in a bunch of commands when I realize that I am going to keep typing them in and that I would really like a script. Now I understand that no matter how I save my history, I am ...
5
votes
1answer
756 views

history list in bash always truncated to 5000 lines at new login

Similar to the 5000 line limitation problem when first thing in .bashrc I have export HISTSIZE=10001 and export HISTFILESIZE=$HISTSIZE My users do not have ~/.inputrc files and the system-wide /...
1
vote
4answers
6k views

Count number of lines in files then compare which has more (BASH) [duplicate]

I need to count the number of lines in x files and compare which has more. The one I've done only takes two files and compares them. Any idea how to make it x amount of files? echo Enter the ...
3
votes
3answers
3k views

Extracting text from a text file in bash

I have a large text file that is all one line. In the text are several sections that looks like foo=12345 and I need to extract them all as separate lines, ignoring the rest of the file. For example:...
9
votes
2answers
11k views

Excluding a file in bash loop

In the following script, How do I exclude a directory in the loop? I want to do this because all files are symlinks pointing to this directory that exists in the same level, so I don't want to add it. ...
0
votes
1answer
144 views

terminal logging doesn't complete when I close the terminal

This is the last line in my .bashrc (lines breaks inserted for readability): STARTTIME=`date +%F-%Hh-%Mm-%Ss-%N`; \ script -q -t 2> /home/USER/logs/$STARTTIME-timing.txt \ -c 'bash --rcfile ...
1
vote
4answers
943 views

Exporting environment variable

I want to export an environment variable whose value contains a variable but I don't want that variable (which is within the value) to be evaluated during the export process. i.e. export STY=for i in ...
4
votes
2answers
36k views

Auto running bash script on login

I wanted to make a script which ran automatically on login so I put it into the file ~/bash.profile, but it didn't run. When I put it in bashrc, it ran on opening a terminal. What I was doing in the ...
4
votes
2answers
3k views

Insert a multiline string into another string

I need to insert lines into an xml file : Insert <one> </one> into <tags> </tags> To obtain <tags> <one> </one> </tags> I tried this: ...
2
votes
2answers
919 views

Variable name from command output

I have some string, and want to split on colon ":" assigning on variable with name from left part and value from right part. For example: echo "Title: Some title" | sed 's/:.*//' gives me wanted ...
6
votes
3answers
2k views

How to perform an action only on the first line?

sed 's/.\(.*\)/\1/' myfile Say myfile contains: abc def ghi I want to remove the first character only from the first line but the above removes it from all the line.
25
votes
6answers
22k views

How can I open a new terminal in the same directory of the last used one from a window manager keybind?

I'm using a tiling window manager and I switched from gnome-terminal with multiple tabs to multiple urxvt instances managed by the window manager. One of the features I miss is the ability to open a ...
30
votes
3answers
10k views

Excluding some of the commands from being getting stored in bash history

Is there any way to exclude commands like rm -rf, svn revert from being getting stored in bash history? Actually I, by mistake, have issued them a number of times even though I have no intent to do, ...
13
votes
4answers
23k views

What are the different ways that a message can be displayed to a bash shell after a user logs in?

I have a CentOS 5.7 VPS using bash as its shell that displays a branded greeting immediately after logging in via SSH. I've been trying to modify it, but can't seem to find where it is in the usual ...
70
votes
7answers
18k views

Set and Shopt - Why Two?

set and shopt are both shell builtins that control various options. I often forget which options are set by which command, and occasionally which option sets/unsets (set +o/-o, shopt -s/-u). Why are ...
0
votes
5answers
130 views

When I set the default shell, it doesn't “take”

I want my default shell to be bash. However, whenever I log into my server, the shell is apparently -bash: $ echo $0 -bash If I run bash, I'm of course switched to bash for that session, but it ...
0
votes
2answers
2k views

How to calculate in linux number of file inside nested directories?

I need to extract information how much files are in some directory tree: /.../testRoot/test1/test11/..../file1 /.../testRoot/test1/test11/file2 /.../testRoot/test1/test11/..../file3 /.../...
1
vote
2answers
2k views

Permanently Storing a command in bash history

Is there any way to store a command in bash history permanently? The actual scenario goes like this:- I have a project in C and in order to configure my project, I have to run a very long command ...
3
votes
2answers
3k views

Keep global variables values piping through functions

I wrote a little bash script using sed on some html pages to extract some urls. To avoid each time grabbing sed results in a variable then read it again I simply made 3 functions and piped together. ...
3
votes
1answer
858 views

Bash: Filter invalid commands from history, specifically passwords

Related Question, less specific: Automatic cleanup of Bash history This question focuses on a single problem. Over time, a user will eventually enter a password on the command line by accident, and ...
5
votes
3answers
2k views

Conflict between `pushd .` and `cd -`

I am a happy user of the cd - command to go to the previous directory. At the same time I like pushd . and popd. However, when I want to remember the current working directory by means of pushd ., I ...
2
votes
2answers
943 views

inserting a token while parsing a file - bash

I have a file with this structure: ma${token}jid-azimi-865131021 javad-ya${token}rip-865132012 and here is my program: IFS="-" token="12345678" cat input.txt | while read col1 col2 col3; do ...
16
votes
1answer
3k views

Why does a=0; let a++ return exit code 1?

Try it: $ a=0 $ let a++ $ echo $? 1 # Did the world just go mad? $ echo $a 1 # Yes, it did. $ let a++ $ echo $? 0 # We have normality. $ echo $a 2 Contrast with this: $ b=0 $ let b+=1 $ echo $? 0 ...
113
votes
3answers
425k views

Pass command line arguments to bash script

I am new to bash script programming. I want to implement a bash script 'deploymLog', which accepts as input one string argument(name). [root@localhost Desktop]# ./deploymLog.sh name here I want to ...
0
votes
1answer
1k views

Shell script executing in the terminal but not from shell script file [duplicate]

Possible Duplicate: How can I make variables “exported” in a bash script stick around? I have a problem with executing script from file. When I type in command line PATH=$PATH:/home/...
12
votes
3answers
7k views

Dereference concatenated variable name

I can do this, but it requires making a string of the variable then dereferencing it. Is there any way to shorten it to a simpler statement? #!/bin/bash FRUITS="BANANA APPLE ORANGE" BANANA_COLOUR="...
-1
votes
2answers
659 views

Shell programming temp=$1 vs temp=1

What's the difference between temp=$1 and temp=1? Why do I need the dollar sign? For example: #!/bin/bash temp=$1 cell=$((($temp-32)*5/9)) echo $cell
85
votes
4answers
34k views

Why does parameter expansion with spaces without quotes work inside double brackets “[[” but not inside single brackets “[”?

I'm confused with using single or double brackets. Look at this code: dir="/home/mazimi/VirtualBox VMs" if [[ -d ${dir} ]]; then echo "yep" fi It works perfectly although the string contains a ...
4
votes
1answer
579 views

Renaming files from bash in linux

There is a bunch of files of the following format hh-mm-ss.png where hh, mm, ss are hour, minute and second. How can I rename all these files in the directory in a sequential way from 1.png til n.png ...
5
votes
2answers
5k views

Admin user not showing directory path in console prompt, only $

I have an Ubuntu server that I administer through ssh. When I login as root I get a full path in the prompt, like /var/www. I created a user with sudo privileges for the purpose of administration. ...
2
votes
3answers
1k views

In bash shell, how to insert the previous line inside the current line?

In bash, how to expand the !! in command line while interactively editing the command inside shell? I am working in vi edit mode. When typing a new command line, I want to introduce the previous line ...
6
votes
1answer
212 views

How to find bash in a portable way

When we write shell scripts we do this: #!/bin/bash But What if bash is in /usr/bin/? How can we find it in a portable way inside script? I did this, but it gives error: #!which bash
9
votes
1answer
36k views

Using regex inside if clause in bash

Look at this if block: #!/bin/bash str="m.m" if [[ "${str}" =~ "m\.m" ]]; then echo "matched" else echo "not matched" exit 1 fi exit 0 This should print "matched", but it doesn't. ...
2
votes
2answers
3k views

using read inside if clause - bash

I have a script: #!/bin/bash if [[ read -t 10 -sp "Enter Password: " passwd ]]; then echo -e "\nPassword: ${passwd}" else echo -e "\nInput timed out" >&2 exit 1 fi exit 0 But it ...
1
vote
3answers
1k views

Problem with bash script executed from website

I have just begun writing scripts in bash and I am putting some of them on my web server (whihc is located on my virtual Debian 6.0.4 machine). I have installed lighttpd on this machine.and when I ...
49
votes
2answers
16k views

Why is bash's prompt variable called PS1?

The environment variable for the bash prompt is called PS1 (usually set in ~/.bashrc). What does PS1 stand for? Is there a PS2?
1
vote
1answer
450 views

selective stdout , stderr and logging using the script command [duplicate]

Possible Duplicate: Show only stderr on screen but write both stdout and stderr to file stdout , stderr and logging using the script command cat test.sh: rm -v foo.tmp date pwd cat test2.sh: ...
3
votes
3answers
429 views

How do I easily update list of md5sums?

Sometimes I use an unreliable medium (flash) to store a good deal of data. To at least recognise bit flips I store a file with the md5sums alongside. This file is usually created by a variation of ...
188
votes
21answers
260k views

How to add a newline to the end of a file?

Using version control systems I get annoyed at the noise when the diff says No newline at end of file. So I was wondering: How to add a newline at the end of a file to get rid of those messages?
2
votes
3answers
272 views

Selecting latest files and grouping by name

I have a directory containing files of the following name structure. <device>.<yyyy-mm-dd-hh-mm-ss> I am working on a script that will retain the the last X copies of these ...
7
votes
3answers
2k views

Why are background jobs started by my shell living longer than the parent shell and controlling terminals?

It is commonly said that "&" puts the process into background execution, but that process would be killed when terminal is closed. But from what I see, everything I sent into background was ...
8
votes
4answers
10k views

Set Variable Environment Variables in bash (or other)

I want my script to read a file containing key/value pairs of environment variables to set, and then to set them. So far, I have this: #!/bin/bash cat $1 | while read kv do key=${kv%=*} val=...