Find an Online Tutor Now

41 Answered Questions for the topic Bash

Bash Python Perl


Pcregrep vs Perl vs Python for bash scripting when needing advanced regex features?

I've lately been using `pcregrep` to do Perl-style group matching when doing my bash scripts.The problem with `pcregrep` is that it's not readily available on Linux machines in general.An... more
Bash Linux File


How to create a file in Linux from terminal window?

What's the easiest way to create a file in Linux terminal?


How to count lines in a document?

I have lines like these, and I want to know how many lines I actually have... 09:16:39 AM all 2.00 0.00 4.00 0.00 0.00 0.00 0.00 0.00 94.00 09:16:40 AM all 5.00 0.00 0.00 4.00 0.00 0.00 0.00... more


Print a file skipping first X lines in Bash?

I have a very long file which I want to print but skipping the first 1e6 lines for example. I look into the cat man page but I did not see any option to do this. I am looking for a command to do... more
Bash Linux Shell


./configure : /bin/sh^M : bad interpreter?

I've been trying to install lpng142 on my fed 12 system. Seems like a problem to me. I get this error [root@localhost lpng142]# ./configure bash: ./configure: /bin/sh^M: bad interpreter: No such... more
Bash Linux Shell


How do I test if a variable is a number in Bash?

I just can't figure out how do I make sure an argument passed to my script is a number or not. All I want to do is something like this: test *isnumber* $1 && VAR=$1 || echo "need a... more


How can I change the default Python version on Snow Leopard?

I recently upgraded my Mac OS X 10.5 Leopard install to 10.6 Snow Leopard, and with that came an upgraded version of Python, 2.6.1 (instead if 2.5.1). Now when I type `python` in the Terminal i... more
Bash Linux Shell


Defining a variable with or without export?

What is `export` for? What is the difference between: export name=value and name=value


Get current time in seconds since the Epoch on Linux, Bash?

I need something simple like `date`, but in seconds since 1970 instead of the current date, hours, minutes, and seconds. `date` doesn't seem to offer that option. Is there an easy way?
Bash Linux Stream


How can I redirect and append both stdout and stderr to a file with Bash?

To redirect *stdout* to a truncated file in Bash, I know to use: cmd > file.txt To redirect *stdout* in Bash, appending to a file, I know to use: cmd >> file.txt To redirect both... more
Bash Linux Shell


What does set -e mean in a bash script?

I'm studying the content of this **preinst** file that the script executes before that package is unpacked from its Debian archive (.deb) file. The script has the following code: #!/bin/bash ... more
Bash Linux Cp


How to have the cp command create any necessary folders for copying a file to a destination?

When copying a file using `cp` to a folder that may or may not exist, how do I get `cp` to create the folder if necessary? Here is what I have tried: <!-- language:none --> [root@file... more
Bash Linux Shell


Get program execution time in the shell?

I want to execute something in a linux shell under a few different conditions, and be able to output the execution time of each execution. I know I could write a perl or python script that would... more
Bash Linux


Bash ignoring error for a particular command?

I am using following options set -o pipefail set -e In bash script to stop execution on error. I have 100 of script executing and I don't want to check return code of the script. But for a... more


Why do you need to put #!/bin/bash at the beginning of a script file?

I have made [Bash][1] scripts before and they all ran fine without this at the beginning. What's the point of putting it in? Would things be any different? Also, how do you pronounce `#`? I know... more
Bash Linux Unix


Extract file basename without path and extension in bash?

Given file names like these: /the/path/foo.txt bar.txt I hope to get: foo bar Why this doesn't work? #!/bin/bash fullfile=$1 fname=$(basename $fullfile) fbname=${fname%.*} echo... more
Bash Linux Unix


Given two directory trees, how can I find out which files differ?

If I want find the differences between two directory trees, I usually just execute: diff -r dir1/ dir2/ This outputs exactly what the differences are between corresponding files. I'm interested... more
Bash Linux Unix


How can I format my grep output to show line numbers at the end of the line, and also the hit count?

I'm using grep to match string in a file. Here is an example file: example one, example two null, example three, example four null, `grep -i null myfile.txt` returns example two null, ... more
Bash Unix Shell


How to represent multiple conditions in a shell if statement?

I want to represent multiple conditions like this: if [ ( $g -eq 1 -a "$c" = "123" ) -o ( $g -eq 2 -a "$c" = "456" ) ] then echo abc; else echo efg; fi but when I execute the script,... more
Bash Unix Arrays


Loop through an array of strings in Bash?

I want to write a script that loops through 15 strings (array possibly?) Is that possible? Something like: for databaseName in listOfNames then # Do something end
Bash Linux Unix


How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script?

How can I programmatically (i.e., not using `vi`) convert DOS/Windows newlines to Unix? The `dos2unix` and `unix2dos` commands are not available on certain systems. How can I emulate these with... more
Bash Unix File


How to split a large text file into smaller files with equal number of lines?

I've got a large (by number of lines) plain text file that I'd like to split into smaller files, also by number of lines. So if my file has around 2M lines, I'd like to split it up into 10 files... more
Bash Linux Io


How to redirect output to a file and stdout?

In bash, calling `foo` would display any output from that command on the stdout. Calling `foo > output` would redirect any output from that command to the file specified (in this case... more
Bash Linux Unix


How can I recall the argument of the previous bash command?

Is there a way in Bash to recall the argument of the previous command? I usually do `vi file.c` followed by `gcc file.c`. Is there a way in Bash to recall the argument of the previous command?


How do I prompt for Yes/No/Cancel input in a Linux shell script?

I want to pause input in a shell script, and prompt the user for choices. The standard 'Yes, No, or Cancel' type question. How do I accomplish this in a typical bash prompt?

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.


Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.