43 Answered Questions for the topic Bash

Bash Python Perl

07/29/19

#### 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

07/05/19

#### How to create a file in Linux from terminal window?

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

07/05/19

#### 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

07/04/19

#### 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

06/30/19

#### ./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

06/27/19

#### 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

06/26/19

#### 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

06/24/19

#### Defining a variable with or without export?

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

06/20/19

#### 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?

06/20/19

#### 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 Sh

06/12/19

#### 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

06/12/19

#### 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

06/11/19

#### 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

06/05/19

#### 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

06/04/19

#### 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

06/02/19

#### 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 Diff

05/30/19

#### 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 Grep

05/29/19

#### 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

05/27/19

#### 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

05/26/19

#### 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

05/21/19

#### 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

05/21/19

#### 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

05/17/19

#### 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

05/16/19

#### 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?

05/13/19

#### Replacing some characters in a string with another character?

I have a string like <!-- language: lang-none --> AxxBCyyyDEFzzLMN I want to replace all x and y and z with _ so that the output is A_BC_DEF_LMN How to do that? I know a... more

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

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

#### OR

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