All Questions

Tagged with
Filter by
Sorted by
Tagged with
0 votes
1 answer
28 views

GNU parallel export function output to variables failed

This script is to determine when destination file already exist, source file will update the destination one or be removed according to flag "$dup_act". #!/bin/bash dup_chk() { # $1: f_src,...
user avatar
0 votes
1 answer
53 views

Is there a way to export functions with parameters already expanded?

Let's say I have two files main.sh and sub.sh in the same folder with the following contents: main.sh: #!/usr/bin/env bash export PARAMETER="main" my_func(){ echo "$PARAMETER $1"...
user avatar
  • 3
2 votes
0 answers
64 views

Why `declare -p VAR` returns "declare -- VAR" on an unset local variable but gives an error "bash: declare: A: not found" on an unset global variable?

I don't understand: $ declare -i VAR=0; \ > echo "$A"; \ > fun() { local -i VAR=1; echo {; echo "$VAR"; declare -p VAR; unset VAR; echo "$VAR"; declare -p VAR; echo ...
user avatar
  • 234
0 votes
1 answer
34 views

files zipped using function not naming correctly

I'm probably missing something basic with bash function variable syntax here. My understanding of the zip command's syntax is that it's zip newfilename.zip filetobezipped. So I want my function to zip ...
user avatar
  • 3
1 vote
1 answer
189 views

How to display only shell variables (not functions) [duplicate]

How can I display a list of shell variables in Bash, not including functions, which often clutter the output, because of the many related to completion? I have examined declare, and it has an option ...
user avatar
  • 3,277
2 votes
1 answer
100 views

zsh - How do I make local variables available to inner functions that are not defined with 'eval'?

I have a zsh function that looks like this: outer_func () { local LOCAL_VAR='test' inner_func () { echo "${LOCAL_VAR}" } } where $LOCAL_VAR is not available to ...
user avatar
  • 401
0 votes
2 answers
41 views

Function can echo the value that has not received as input

I'm new to bash. I'm confused about the way functions work in this language. I have written this code: #!/usr/bin/env sh choice_func() { echo "$choice" } echo "Enter your choice:" read ...
user avatar
  • 177
1 vote
0 answers
31 views

Variable not available outside function [duplicate]

I'm having difficulties understanding the following example I created. The variable glob_var gets changed inside the test() function, but the original value remains outside of the function: test() { ...
user avatar
  • 400
0 votes
1 answer
579 views

How to pass a multiword string as argument in Linux [duplicate]

I am trying to pass a multiword argument in a function an echo the result in a simple fashion. Currently I am doing this: function myFunction { multiWordString="" for ((i=3; i<=$#; i++...
user avatar
  • 443
0 votes
1 answer
3k views

Random unbound variable error within function

I made a function in bash and when I call it, it crashes with an unbound variable error. I don't understand cause the variables that are said to be unbound are declared. Moreover, it seems to be ...
user avatar
  • 136
1 vote
1 answer
91 views

Can you help me understand this bash behavior? Background processes and their attachment to the current process

I stumbled upon this unexpected behavior and I was hoping someone with a better understanding could explain it! I have a function that is called by a script and run as a background process - ...
user avatar
0 votes
2 answers
164 views

How can I wrap this checking of variable set/unset into a function?

As https://stackoverflow.com/a/13864829/ said, $ if [ -z ${aaa+x} ]; then echo "aaa is unset"; else echo "aaa is set"; fi aaa is unset can test if a variable aaa is set or unset. How can I wrap ...
user avatar
  • 89.7k
10 votes
2 answers
2k views

Can you explain these three things in this bash code for me?

I have a function in my .bashrc file. I know what it does, it steps up X many directories with cd Here it is: up() { local d="" limit=$1 for ((i=1 ; i <= limit ; i++)) do ...
user avatar
1 vote
4 answers
103 views

Pass function argument to defined variable

How can I "inject" a function argument to a defined variable like in this example? mood="i am $1 happy" happy () { echo "$mood" } happy "very" Current output: i am happy Desired output: i ...
user avatar
  • 13
0 votes
1 answer
248 views

Shell script: Call variable with parameters/arg

I have the following function in a bash script: testcur.sh : #!/bin/bash function valcurl { if [[ $1 != "" ]] then tbl=$2 # can be multiple values data=/home/data ...
user avatar
3 votes
2 answers
709 views

Zsh, Indirect array variable assignment without using eval

I have a variable VARNAME which contains a name of another variable. I'd like to assign to this another variable without using eval. How can I do that? Th reason I don't want to use eval is the ...
user avatar
  • 412
3 votes
3 answers
2k views

How to assign the success/fail status of a function to a variable in Bash?

I know that a way to do it is : # run a command here, like [[ $A == *"ABC"* ]] result=$? if (( result == 0 )) then result=true else result=false fi Then I can do : if $result then ... Is there ...
user avatar
-1 votes
2 answers
286 views

When is a variable local and when global?

Consider the following two functions: f1() { if [ "$a" == "" ]; then a="0"; else a=$(($a+1)); fi; echo "$a"; } f2() { echo "f1(): $($1)"; } If I call f1 multiple times a will be ...
user avatar
  • 1,151
4 votes
1 answer
4k views

Bash local variable initialization

How does Bash initialize local variables? Will the following commands always do the same thing (when used inside a function)? local foo local foo= local foo=""
user avatar
  • 3,744
1 vote
1 answer
379 views

How to know where shell variables and functions are set?

When I type the set command in my system I've got this extract out : __colormgr_commandlist=' create-device create-profile delete-device delete-profile device-add-profile ...
user avatar
  • 1,288
1 vote
1 answer
2k views

How to use ssh in a function(bash)?

I am trying to execute few commands in a server by logging in using sshpass command like below. SSH_ARGS='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -q' sshpass -p 'password' ssh ${...
user avatar
  • 882
1 vote
1 answer
410 views

Declare as local var will break a function and log out "1: number expected"

function projectopen { local di_files=(*.xcworkspace */*.xcworkspace *.xcodeproj */*.xcodeproj) # open first exsit file ls -d -f -1 $di_files 2>/dev/null \ | head -1 \ | xargs ...
user avatar
  • 235
8 votes
1 answer
1k views

Circular name references in bash shell function, but not in ksh

I'm writing a set of shell functions that I want to have working in both Bash and KornShell93, but with Bash I'm running into a "circular name reference" warning. This is the essence of the problem: ...
user avatar
  • 267k
1 vote
1 answer
267 views

Func name as variable in loop

Overview: I save my variable in a config file and call them later. Each entry with the name FailOverVM has a number beside it like FailOverVM1 and I want to check to see if it has data and generate a ...
user avatar
2 votes
1 answer
739 views

Save return value from a function in one of its own parameters

This is what I want to achieve: Function: Func1() { $1="Hello World" } Call function: local var1 Func1 var1 echo $var1 (should echo Hello World) I found this example which seems to work, but ...
user avatar
  • 530
50 votes
4 answers
67k views

Scope of Local Variables in Shell Functions

After reading 24.2. Local Variables, I thought that declaring a variable var with the keyword local meant that var's value was only accessible within the block of code delimited by the curly braces of ...
user avatar
  • 601
4 votes
1 answer
3k views

how to get or reflect the name of the bash function which is called? [duplicate]

i did not yet found a solution to this. Anyone a hint? i sometimes write bash functions in my shell scripts and i love to have my scripts being verbose, not just for debugging. so sometimes i would ...
user avatar
1 vote
1 answer
183 views

Which shells have functions where "local" does not alter exported variables for child processes?

In the example below, an exported variable is re-set as local in a function. Bash, Zsh, Fish don't pass on the original value to the child process. Are there any shells that make local affect the ...
user avatar
  • 739
15 votes
3 answers
63k views

How to increment local variable in Bash?

Data 1 \begin{document} 3 Code #!/bin/bash function getStart { local START="$(awk '/begin\{document\}/{ print NR; exit }' data.tex)" echo $START } START2=$(getStart) echo $START2 ...
user avatar
0 votes
3 answers
3k views

Returning local values from Bash variables?

I studied this article called Returning Values from Bash Functions. Data Lorem. \begin{document} hello \end{document} Case #1 which does not work Code #!/bin/bash function getStart { ...
user avatar
9 votes
4 answers
14k views

How to use call-by-reference on an argument in a bash function

I am trying to pass a "var name" to a function, have the function transform the value the variable with such "var name" contains and then be able to reference the transformed object by its original "...
user avatar
1 vote
1 answer
1k views

Why doesn't my function work with spaces? (cd, dirname) [duplicate]

I have had this function I use it very often and it works fine. Here it is: cdx () { cd `dirname $1` ; } However, this does not work with spaces. When I use it like this for example cdx ~/desktop/...
user avatar
  • 10.6k
4 votes
1 answer
7k views

How do I define alias with variables which can be changed at runtime?

This is probably a very easy to answer question, but I could not find any questions already asking this due to different wording when writing titles. Running help alias on my bash prompt returns only ...
user avatar
  • 10.6k
0 votes
1 answer
4k views

Value assigned inside a function variable is always empty

I have the following simple script. In this script, I am assigning a value to a global variable inside a function. I can clearly see that the value is being assigned to the variable via a debug ...
user avatar
  • 963
2 votes
2 answers
368 views

Nature of the positional parameters

I wrote this shell script which sort of confused me a bit... function func { the variables received are echo $0: $1 and $2 } echo in the main script func ball boy The name of script is shell.txt I ...
user avatar
  • 345
0 votes
2 answers
123 views

Why must you be careful when using Bash's built in command history function to re-run previous commands that contain variables?

I know !! re-runs commands but what exactly would occur if I re-ran a command that had a variable in the command?
user avatar
  • 205
4 votes
4 answers
5k views

Scope of variables when calling function from find

In a bash script I define a function that is called from find. The problem is that the scope of variables does not extend to the function. How do I access variables from the function? Here is an ...
user avatar
  • 553