Questions about bash only, as opposed to other Bourne/POSIX shells. For all Bourne/POSIX-style shells, use the /shell tag instead.

learn more… | top users | synonyms

6
votes
5answers
226 views

Strategy to extract movies's name from this ununiform dataset?

I am working on a movie database problem to improve regular expressions, this is the problem I'm running into. My dataset looks like this: Movie Name (variable space and tabs) year Movie1(can ...
28
votes
5answers
689 views

How to save a command you entered without executing it? [duplicate]

Every Linux user has experienced this annoying thing: you begin typing a long and boring command, then realise you should have executed another one before. How to save the first one to execute it ...
4
votes
3answers
185 views

How can I delete a trailing newline in bash?

I'm looking for something that behaves like Perl's chomp. I'm looking for a command that simply prints its input, minus the last character if it's a newline: $ printf "one\ntwo\n" | ...
4
votes
2answers
382 views

write a line before it is executed without writing it twice

I write a bash script with a --verbose option, where I want to write most lines before I execute them xinput --set-prop "$TP" "Device Accel Constant Deceleration" $TOUCHPAD_DECELERATION I would ...
2
votes
1answer
35 views

Bash for loop create folders; do I have to escape some chars?

I want to create 5 files with the same name but with an incremental counter. If I do: for i in {1..5}; do touch hallo_$i; done I get hallo_1, hallo_2, hallo_3, etc. If I do: for i in {1..5}; ...
1
vote
1answer
22 views

default argument parsing section

I created a bash script where I would like to add a help options like -h, --help, --verbose and some others. If I create it like described here Will this be a standard solution? # Execute getopt on ...
2
votes
0answers
44 views

Bash tabcomplete file names for environmental variable values

After I upgraded my Ubuntu from 12.04 to 14.04, bash would no longer let me tabcomplete file names when setting the value of an environmental variable, e.g. FILE=/home/_ _ is where I would press ...
0
votes
2answers
62 views

Convert json mapping object to managed csv row using bash script

I have json mapping under properties key as below in a file Customer.json, { "customer": { "properties": { "customerId": { "type": "string", ...
0
votes
2answers
98 views

How do I enable &>/dev/null by default for all sh/bash commands?

Is there a way to make &>/dev/null the default behavior in a sh/bash script? I' rather not have to appended this to every command within the console, not an *.sh script. I'm looking to make a ...
12
votes
3answers
280 views

Using variables to store terminal color codes for PS1?

In my .bashrc, I use ANSI terminal color codes to colorize various bits. It looks like this: PS1='\u@\h:\w\[\033[33m\]$(virtual_env)\[\033[32m\]$(git_branch)\[\033[0m\]$ ' where virtual_env and ...
14
votes
2answers
402 views

How do I start all shell sessions in a directory other than $HOME?

When I open my non-login shell in Ubuntu, my present working directory is /home/user_name (my $HOME environment variable), but I want to change this such that when I start my terminal I am in some ...
2
votes
2answers
46 views

Remove files and directories even if not empty, except some directories

I am trying to remove all contents of a directory with some exclusions. I'm currently using this: find . -type f | grep -vZf ../../exclude.file | xargs rm -rf The exclude.file contains: *log* ...
5
votes
3answers
134 views

Combining brace expansion and output redirection

Often, I use parameter expansion on the command-line, such as cp a.{ext1,ext2} which is expands to cp a.ext1 a.ext2 However, in many cases I do not use cp, but another tool, let's call it cat, ...
2
votes
1answer
62 views

Could “>” be included in a bash Variable?

Current approach: sudo awk 'BEGIN{printf "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<hello>\n\t<world>\n";} {print "\t\t",$0}END{printf "\t </world>\n</hello>\n"}' ...
0
votes
0answers
33 views

Error unzipping file through ksh, unzip works through bash

I have an auto-generated zipfile foo.zip on a Red Hat server that needs to be extracted to a particular location /bar. This is done through an install script using ksh. When the script executes unzip ...
0
votes
2answers
57 views

Why do I have to execute bash programs with ./? [duplicate]

Why is it that some bash programs will only run on my system when I type ./ before their name? An example: In my [...]/android/sdk folders I have to execute adb like this: ./adb devices Why not ...
4
votes
1answer
127 views

Is there some way to make alias expansion work inside if…fi on Bash?

Check this script: #!/bin/bash if true;then alias WeirdTest='uptime';shopt -s expand_aliases WeirdTest fi WeirdTest The first time WeirdTest is executed it says "command not found".
2
votes
1answer
39 views

Strip color on OS X with BSD sed (or any other tool)

I'm on OS X, which has a BSD version of sed (which I'm guessing is inferior to a GNU version on Linux), and all the techniques for removing color on commandlinefu.com did not work. I tried replacing ...
2
votes
2answers
90 views

Change directory based on a pipeline's output

I want to go to a directory with using filter For example there is a file named this-is-awsome ls | grep this-is-awsome | xargs cd How can I go to a directory with filter?
3
votes
3answers
105 views

Messed up bash.bashrc file, commands not working anymore

Trying installing Java, I tried to add it to the executable path with the line: export PATH=/usr/lib/jvm/jdk1.7.0_60/bin:$PATH in the system-wide /etc/bash.bashrc file. For some reason this ...
5
votes
1answer
36 views

Ctrl-C is ignored by pppd when put in a shell script

I'm trying to connect to GPRS network through a serial port connected GSM modem. When I call /usr/sbin/pppd call <peer_name> from the command line, it correctly receives and handles Ctrl+C from ...
2
votes
1answer
43 views

diff two directories for changes and format output to use for script

I currently try to monitor two directories and subdirectories for changes in files. The directories contain the same set of files, some of them are changed. So I thought I might use the diff command ...
3
votes
2answers
29 views

How can I list the info for an array of /dev/disks using bash expansion or substitution?

I want the output of: diskutil list; diskutil info [multiple devices] Without having to do: diskutil info disk0; diskutil info disk0s1; diskutil info disk1 ...etc For example, with many builtin ...
4
votes
1answer
90 views

How to make a variable from a subshell available in the parent shell

I've written a quick-and-dirty script to time some reports from a web service: BASE_URL='http://example.com/json/webservice/' FIRST=1 FINAL=10000 for report_code in $(seq 1 $FINAL); do (time -p ...
6
votes
1answer
55 views

BASH return to main function

I have a BASH script that calls a function, which calls other functions: #!/bin/bash function foo { function bar { # do something } bar } foo How can I return from bar ...
3
votes
2answers
29 views

set verbose prints unrelated extra lines

Using set -x causes my shell to print unrelated stuff. For example: $ set -x ++ printf $'%157s\r' $ echo 'hello' + echo hello hello ++ printf $'%157s\r' The lines starting with '++' are extra. How ...
1
vote
1answer
46 views

Bash script and stdin

I am having a C program which requires to read from stdin. This is one of the requirements, and it cannot change. I have written a simple bash script that creates all the directories I want, handles ...
2
votes
1answer
132 views

What is the difference between filename=${1:-/etc/hosts} and filename=/etc/hosts? [duplicate]

What is the difference between filename=${1:-/etc/hosts} and filename=/etc/hosts? For example: filename=/etc/hosts if [ -r "$filename" ] && [ -s "$filename" ]; then md5sum $filename ...
1
vote
2answers
46 views

Using exec in find over ssh from shell script

So, I am attempting to execute the following from within a shell script; ssh -q $CUR_HOST "cd $LOGS_DIR; echo cd $LOGS_DIR; find . -name *.log -mmin +1440 -exec gzip {} \; exit" When this runs, it ...
2
votes
1answer
95 views

Bash script for running python virtual environment does not execute

I am trying to wrap the following call into a script, which runs my Python virtual environment: [lucas@lucas-ThinkPad-W520]/home/lucas/bin$ source ...
8
votes
4answers
97 views

Save output of command that modifies environment into a variable

How to save output of a command that modifies environment into a variable? I'm using bash shell. Assume that I have: function f () { a=3; b=4 ; echo "`date`: $a $b"; } And now, I can use commands ...
0
votes
1answer
33 views

BASH paste sourced files into main source

I have a fairly large BASH project, which I split into multiple files to make it more readable. As a result, the main file contains many source statements. Now I'm releasing it on GitHub and I'm ...
5
votes
8answers
344 views

Remove last character from an echo variable

This script works great and I modified it to increment by 5. My problem is I do not need the last comma at the end and I'm not sure how to remove this. for ((i=1, j=0; i <= 12 ; i++, j=j+5)) do ...
2
votes
3answers
45 views

Get the last minute dated lines from list

If I have an input file with a list of users and their dates they were last signed in, how can I select with a sort | awk function the last minute lines? The dates are written in a +%H:%M:%S format. ...
1
vote
1answer
22 views

trapping signals inside grouping commands

I recently ran into a strange behavior. Trap doesn't get executed if inside grouping command: $ cat ./1.sh #!/usr/bin/env bash set -eu trap 'echo exit' EXIT $ ./1.sh exit $ cat ./2.sh #!/usr/bin/env ...
2
votes
1answer
79 views

how to execute more than one command gnome-terminal

I have a script which is running some of (more than 15) applications like: cd mypath sleep 1 gnome-terminal -t App1 --tab -e "./App1" sleep 1 gnome-terminal -t App2 --tab -e "./App2" .. .. sleep ...
1
vote
1answer
40 views

Create a log file with the same name as input file

I'm new to linux shell scripting. I have few input files that I have to parse one by one. So in my "work in progress" WIP folder I have one input file at a time. While one file is under processing I ...
0
votes
4answers
124 views

Bash if conditions of variables within intervals

I have some data spread over time intervals, and I want to take some of those data within the time intervals. For example, I have data at times within 1..9, 11..19, etc., and I want to take data ...
0
votes
0answers
25 views

Parameters files, pfiles director: how to overwrite default settings

This is a quite general question that I am not sure to be able to address correctly. I must manage some bash or tcsh scripts that invoke other software's tools. These tools are invoked by name and ...
3
votes
2answers
51 views

awk: Blank line with date

I use the following code to write the online users' process numbers and the date they were checked: w | tail -n +3 | awk ' { "date +%H:%M:%S" | getline tim} {if ($1 != "") ...
1
vote
5answers
88 views

How to copy a file from a remote network to the local desktop

So I used SSH to remotely login to another computer on a network. I have many files that I would like to copy over to my local desktop. I am trying to follow this How-to : However, I think one ...
9
votes
4answers
322 views

Recursively iterate through files in a directory

Recursively iterating through files in a directory can easily be done by: find . -type f -exec bar {} \; However, the above does not work for more complex things, where a lot of conditional ...
2
votes
1answer
68 views

Integer part from float operations without | bc

I read a billion of discussion about this but still have confused thoughts. I just need to use bash to calculate some parameters, and for this operation I need to get the integer part of a float ...
0
votes
1answer
19 views

user input not accepted in nested case statement

I am trying to create a nested case statement in which user input is expected (Y/N). However, the system never waits for the input and always goes to the third option, "Please answer yes or no". Can ...
4
votes
1answer
52 views

keep aliases when I use sudo bash

I have a Centos 5.5 server that has my own alias commands for my account. When I do "sudo bash" I can still use those aliases, I didn't have to add the aliases to the root bashrc. I have another ...
2
votes
2answers
17 views

Clone repos with a similar name

I would like to do something like: git clone https://github.com/meteor{A,B,C,D}test but bash does not convert the {} into an each. What am I doing wrong?
2
votes
3answers
38 views

Reuse similar flags for multiple commands

Let's say I want to execute several commands with similar flags such as: du -sh / --exclude=/dir1 --exclude=/dir2 --exclude=/dir3/dir4 tar cvf archive / --exclude=dir1 --exclude=dir2 ...
8
votes
2answers
344 views

disable CTRL-D window close in terminator (terminal emulator)

I am often logged in through multiple SSH sessions. To logout from my sessions, I press CTRL+d, until I am back on my local machine. However, sometimes I press once too many, and my terminal exits. ...
0
votes
1answer
38 views

How to make xargs ping and head output as expected?

Given an array of domains I would like to obtain their IP addresses, in the following format: 98.139.183.24 98.137.236.24 This script #!/bin/bash domains=(yahoo.com yahoo.ca) printf "%s\0" ...
7
votes
6answers
722 views

How to read first and last line from cat output?

I have text file. Task - get first and last line from file after $ cat file | grep -E "1|2|3|4" | commandtoprint $ cat file 1 2 3 4 5 Need this without cat output (only 1 and 5). ~$ cat file | ...