2
votes
1answer
44 views

Parameter substitution and error messages: suppressing line numbers etc

Here is my example file: !/bin/bash # argument-one # Is first argument missing? # First method [[ "$1" == "" ]] && echo "Usage: $(basename $0) filename"; exit 1 # Second method # ...
3
votes
1answer
46 views

Customize bash to always include a certain parameter

When using the "ls" command, bash prints in the following format: file1 file2 file3 file4 file5 file6 file7 file8 But when given the parameter "-1", it prints: file1 file2 file3 etc... Is there ...
2
votes
2answers
101 views

What is the bash syntax for extracting the values from multiple instances of the same argument?

I want to use multiple instances of command line paramater such as the -d option used by PHP for passing PHP options. I am currently using the getopts command in bash. With PHP invocation it would ...
2
votes
2answers
2k views

How do I echo just 1 column of output from ls command?

Lets say when I do ls command the output is: file1 file2 file3 file4 Is it possible to display only a certain column of output, in this case file2? I have tried the following with no success: echo ...
2
votes
1answer
91 views

How to manipulate all parameters of a shell script at once?

While for a finite amount of parameters one can explicitly do something like wrapped_function "${1#prefix}" "${2#prefix}" "${3#prefix}" # etc., isn't there a simpler way to manipulate all ...
1
vote
1answer
161 views

Execute command supplied by function parameters

I'm trying to create a function method in a bash script that executes a command which is supplied to the method by the paramters. Meaning somethings like this: special_execute() { # Some code ...
2
votes
1answer
127 views

prevent trap from altering underscore variable $_

I have the following code in my bashrc to get the execution time of the last command from http://stackoverflow.com/a/1862762 function timer_start { timer=${timer:-$SECONDS} } function timer_stop { ...
4
votes
2answers
183 views

Why is $# always 0 in my function?

Bash is driving me nuts. I can't figure out why the following (nor any of the dozens of variations I've literally copied and pasted from examples) fails to work: #!/bin/bash echo $# function main { ...
2
votes
1answer
186 views

Bash: slice of positional parameters

How can I get a slice of $@ in Bash without first having to copy all positional parameters to another array like this? argv=($@) echo ${argv[@]:2};
2
votes
4answers
295 views

How to write a very simple wrapper that provides default parameters?

Given a program that requires some parameters, e.g. program -in file.in -out file.out, what would be the simple-most approach to write a bash script that could be called with or without any of these ...
3
votes
2answers
276 views

$BASHPID And $$ differ in some cases

I'm reading "BASH pocket guide of Oreilly". It said: The process ID of the current Bash process. In some cases, this can differ from $$. Above explanation , explained $BASHPID variable. ...
2
votes
2answers
129 views

Parameter splitting

I have a bash script that is a simple wrapper around another process: $ cat ~/bin/s3cmd #!/bin/sh trickle -u 80 ~/bin/s3cmd.py $* $ This works great when the parameters don't contain spaces. ...
4
votes
2answers
279 views

Function caller positional parameters

I need to read and write the positional parameters $@ of a function's caller. The Bash man page says that: A shell function is an object that is called like a simple command and executes a ...
1
vote
1answer
537 views

Is it possible to use a parameter within an alias command [duplicate]

Possible Duplicate: How to pass parameters to an alias? As answered in Can less retain colored output? I want to use git diff --color=always filename | less -r to get a colored output of my ...
4
votes
2answers
431 views

Passing parsed output of sed to find (in this direction)

Well, I think you can find dozens of questions on this platform how to pipe find output to sed, but I haven't found anything for the reverse direction so far. What I want to do is modify my input, and ...
11
votes
2answers
1k views

What does `:-` mean in a shell script

I saw this in the end of an awesome shell script but I can't understand the login here because I think it's being short-handed for a longer command. spark ${@:-`cat`} This apears at the end of this ...
5
votes
3answers
3k views

Transform an array into arguments of a command?

I have an array of "options" of a command. my_array=(option1 option2 option3) I want to call this command in a bash script, using the values from array as options. So, command $(some magic here ...
8
votes
1answer
14k views

How to check if there are no parameters provided to a command?

How do you check if $* is empty? In other words, how to check if there were no arguments provided to a command?
13
votes
1answer
492 views

How do ${0##*/} and ${0%/*} work?

I'm quite confused about the following regular expressions I found in a shell script: ${0##*/} ${0%/*} How do they work?
5
votes
3answers
4k views

Print shell arguments in reverse order

I am a bit stuck. My task is to print the arguments to my script in reverse order except the third and fourth. What I have is this code: #!/bin/bash i=$# for arg in "$@" do case $i in ...
3
votes
3answers
2k views

How to pass a string parameter on bash function?

I have this code that does work: get_parameter () { echo "$query" | sed -n 's/^.*name=\([^&]*\).*$/\1/p' | sed "s/%20/ /g" } But I want to replace the "name" with the parameter that I pass ...
12
votes
2answers
2k views

How to safely pass variables to root-enabled scripts?

This question is totally general and not only applicable to my situation, but... I have a small busybox appliance where I want a non-root user to be able to execute a particular script with root ...
7
votes
5answers
2k views

Indexing and modifying Bash parameter array $@

Is it possible to refer to indexes in $@? I can't find any reference to use like the following anywhere in GrayCat's wiki, and the Advanced Scripting Guide and others assign this to a different ...
7
votes
3answers
6k views

How do I split the $0 variable to find directory and relative paths in bash?

The $0 variable contains the path info of the script. How can I change the path info to absolute path? I mean how to process ~, ., .. or similar? How can I split the path info into directory and ...