How can I assign the output of a function *with parameters* to a variable using bash? - bash

Similar to How can I assign the output of a function to a variable using bash?, but slightly different.
If I have a function like this:
function scan {
echo "output"
}
...I can easily assign this to a variable like this:
VAR=$(scan)
Now, what if my function takes one or more parameters, how can I pass them to the function using the "shell expansion" syntax? E.g. this:
function greet {
echo "Hello, $1"
}
# Does not work
VAR=$(greet("John Doe"))
The above produces an error like this with my bash (version 5.0.3(1)-release):
$ ./foo.sh
./foo.sh: command substitution: line 8: syntax error near unexpected token `"John Doe"'
./foo.sh: command substitution: line 8: `greet("John Doe"))'

in this code :
function greet {
echo "Hello, $1"
}
# Does not work
VAR=$(greet("John Doe"))
the error is the parenthesis passing parameters.
try to do this:
function greet {
echo "Hello, $1"
}
# works
VAR=$(greet "John Doe")
it should work.
Explanation: when you use the $ an the parenthesis you have to write inside the parenthesis command as in a shell so the parameters are passed without parenthesis.

I have obviously been writing to much Java code lately. Drop the parentheses when calling the function and everything works flawlessly:
function greet {
echo "Hello, $1"
}
VAR=$(greet "John Doe")

Related

How to give a text file into a shell function?

Hi I'm trying to make a function which should get a text file and then do some things on it and then echo. But when I try to execute it, it says syntax error near unexpected token `"$cat"'
#!/bin/usr/bash
cat=$(< cat_dialogue.txt)
function test_cat (){
echo $1
}
test_cat($cat)
desired output:
>meow meow
Your program may look like the following. Note all differences. Check your scripts with shellcheck.
#!/usr/bin/env bash
cat=$(< cat_dialogue.txt)
test_cat() {
echo "$1"
}
test_cat "$cat"
Here is an example BASH function that strips a branchname:
#create function
function strip () {
#create local variable that takes input and fills $TEXT
local TEXT=$1
#stips the branch number from the branchname
echo $TEXT | sed 's/-[0-9]*//2'
}
strip "testbranch-12345-28796"
hope it helps :) also check the BASH documentation as mentioned by #joshmeranda

Why does bash prohibit a shell function named `df`?

I am using GNU bash, version 4.4.12(1)-release (x86_64-pc-linux-gnu).
I have the following lines in one of my startup files:
df() {
printf "Hello, world!\n"
}
When source that file, I get this error:
-bash: sh/interactive.sh: line 109: syntax error near unexpected token `('
-bash: sh/interactive.sh: line 109: `df() {'
However, if I change the function name from df to dir or ef or anything_else I don't get the error.
I'm assuming that df is somehow a reserved word, but when I checked this list of reserved words in bash I couldn't find it. (And I don't think it deserves to be one, anyway!)
So, can anyone shed some light on this? Why does bash prohibit me from defining a shell function named df?
This happens because you've previously defined an alias for this name. Aliases are simple string prefix substitutions, and therefore interfere with function definitions:
$ alias foo='foo --bar'
$ foo() { echo "Hello"; }
bash: syntax error near unexpected token `('
This is equivalent to (and fails with the same error as)
$ foo --bar() { echo "Hello"; }
bash: syntax error near unexpected token `('
To declare a function with a name that's been overridden with an alias, you can use the function keyword:
$ alias foo='foo --bar'
$ function foo() { echo "Hello, $1"; }
$ foo
Hello, --bar

Random command not found error in shell script [duplicate]

Hi gusy I am trying to learn Bash and cannot seem to get this basic script to work.
#!/bin/bash
function system_info
{
echo "function system_info"
}
$(system_info)
I get a function: command not found issue.
Bash is trying to evaluate the string that is outputted by the system_info function. You'll want to try the following, which will just simply run the function:
system_info
or to store the outputted value to a variable:
value=$(system_info)
You need to invoke the function by saying:
system_info
$(...) is used for command substitution.
Invoke the function inside the script with just the function name and execute the script from the shell
#!/bin/bash
function system_info {
echo "function system_info"
}
system_info
#!/bin/bash
function system_info
{
echo "function system_info"
}
echo $(system_info)
Kind of redundant but it works without the command not found error.
Or This:
#!/bin/bash
function system_info
{
echo "function\n system_info"
}
printf "$(system_info)"
If you want to use newline character.
You can try this code in: https://www.tutorialspoint.com/execute_bash_online.php

Invoke Function through Case statements

Will the below statements work? I am trying to invoke function through Case Statements.
#!/bin/bash
function exit
{
`...`
`...`
`...`}
function start
{
`...`
`...`
}
`Case $input in`
`-book) $(exit) ;;`
`-goal) $(start) ;;`
`*) break ;;`
`esac`
Is the syntax correct?
If you have defined a function:
myfunc() {
echo 'hi'
}
then you can invoke that function in a case statement without a capturing expression. You do it just like any other command:
case "$param" in
expr) myfunc;;
*) echo 'nope';;
esac
You need not use a capturing expression unless you mean it. In your case, what you have would attempt to execute the output of the function as a command itself:
$ double_down() {
> echo 'ping google.com'
> }
$ $(double_down)
PING google.com (74.125.226.169): 56 data bytes
it's possible, but seems unlikely, that you really want this.

Bash Function -> Command not found

Hi gusy I am trying to learn Bash and cannot seem to get this basic script to work.
#!/bin/bash
function system_info
{
echo "function system_info"
}
$(system_info)
I get a function: command not found issue.
Bash is trying to evaluate the string that is outputted by the system_info function. You'll want to try the following, which will just simply run the function:
system_info
or to store the outputted value to a variable:
value=$(system_info)
You need to invoke the function by saying:
system_info
$(...) is used for command substitution.
Invoke the function inside the script with just the function name and execute the script from the shell
#!/bin/bash
function system_info {
echo "function system_info"
}
system_info
#!/bin/bash
function system_info
{
echo "function system_info"
}
echo $(system_info)
Kind of redundant but it works without the command not found error.
Or This:
#!/bin/bash
function system_info
{
echo "function\n system_info"
}
printf "$(system_info)"
If you want to use newline character.
You can try this code in: https://www.tutorialspoint.com/execute_bash_online.php

Resources