All Questions
41 questions
0
votes
2
answers
76
views
Bash scripting: When to use variable, when function?
basic, innocent question:
In bash scripting, why ever using a function, if one can set a variable containing command substitution with the essence of the function - a certain command or set of ...
0
votes
1
answer
258
views
How to store command with option on variable of a function in zsh?
I'm trying to learn bash scripting using freeCodeCamp tutorial for beginners on YouTube.
I'm stuck at the point where he shows how to create a function.
He saved on a variable a command with an option
...
2
votes
1
answer
360
views
printf "%.3f" ${variable with newlines} - error with \n
Like HERE I have a file.csv with numbers in quotes:
"0.2"
"0.3339"
"0.111111"
To round the number (3 decimals) this solutions works great:
printf "%.03f\n" $(...
0
votes
2
answers
52
views
How to pass commands around?
I have this simple script, which does nothing more than:
check if an email matches a specific pattern
in that case, add a tag to a taglist
before quitting, print that taglist
set -e
lista_tag=()
...
0
votes
1
answer
402
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,...
0
votes
1
answer
651
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"...
2
votes
0
answers
125
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 ...
0
votes
1
answer
218
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 ...
4
votes
1
answer
1k
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 ...
2
votes
1
answer
910
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 ...
0
votes
2
answers
168
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 ...
1
vote
0
answers
34
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() {
...
0
votes
1
answer
2k
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++...
0
votes
1
answer
7k
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 ...
1
vote
1
answer
148
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 - ...