All Questions
15 questions
2
votes
1
answer
252
views
BASH: is it possible to change a prompt of function based on its parameter?
I want to construct a function that will change its user input prompt based on its parameter.
my_fucntion takes 1 parameter as db_host after prompting the user for input:
function provide_host () {
...
13
votes
3
answers
26k
views
How to pass all arguments of a function along to another command?
Hello I have this in my ~/.bash_profile
export GOPATH="$HOME/go_projects"
export GOBIN="$GOPATH/bin"
program(){
$GOBIN/program $1
}
so I'm able to do program "-p ...
0
votes
1
answer
357
views
Shell Function: Sequence of Pipelines as Argument
I have a shell function (in .bashrc) which creates a temp file, executes the arguments (including all sequence of pipelines), redirects it to a temp file and then open it in VS Code.
I invoke the ...
4
votes
3
answers
4k
views
Use of positional parameters inside function defintion
How do I use the positional parameters (which are given from the command line) inside of a function declaration?
When inside the function definition, $1 and $2 are the only the positional parameters ...
0
votes
1
answer
5k
views
Syntax error near unexpected token `}' in a Bash function with an if-then statement [closed]
I stored the following script in a file and created an alias to that file in the user's bashrc, then sourced that bashrc:
#!/bin/bash
domain="$1" && test -z "$domain" && exit 2
...
12
votes
4
answers
7k
views
Is there a way to get the positional parameters of the script from inside a function in bash?
The following variables are used to get the positional parameters:
$1, $2, $3, etc.
$@
$#
But they are used for both positional parameters of the script and the positional parameters of a function.
...
23
votes
2
answers
139k
views
How to pass parameters to function in a bash script?
I'd like to write a function that I can call from a script with many different variables. For some reasons I'm having a lot of trouble doing this. Examples I've read always just use a global variable ...
1
vote
1
answer
220
views
$* variable of zsh function leads to unexpected results
I have this function (defined inside my ~/.zshrc):
function graliases
{
if [[ "$#*" -lt 1 ]]
then
echo "Usage: graliases <regex>"
else
echo "$*"
grep -E '*"$*...
1
vote
1
answer
5k
views
Script to send mail using function
I am trying to write a bash script with a function which you use to send an email from the command line to an address and include a Cc address, a subject line, and an input file. For example, if the ...
22
votes
2
answers
24k
views
Optional parameters in bash function
I have a function for quickly making a new SVN branch which looks like so
function svcp() { svn copy "repoaddress/branch/$1.0.x" "repoaddress/branch/dev/$2" -m "dev branch for $2"; }
Which I use to ...
14
votes
1
answer
22k
views
Bash alias with a space as a part of the command
I'm trying to create a bash alias, where the alias itself has a space in it.
The idea is that the alias (i.e. con) stands for sudo openvpn --config /path/to/my/openvpn/configs/. Which results in a ...
1
vote
3
answers
332
views
How can I pass on parameters 4..99 to another function
I'm calling a function and I want to pass up to 100 paramters onto another function. I do not want to pass on the first 3 params, I start with param4 being the first param for the other program.
I ...
23
votes
1
answer
33k
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
...
6
votes
2
answers
1k
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 ...
3
votes
4
answers
12k
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 to ...