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
Related
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")
This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
Closed 3 years ago.
I must be missing something here because no matter what bash compiler I use online and no matter what bash script I write I get the same Unexpected end of file error.
Here's my script
function hello { echo 'hello' } hello
I've also tried this
#!/bin/bash
hello_world () {
echo 'hello, world'
}
jdoodle.sh: line 2: $'\r': command not found
jdoodle.sh: line 3: syntax error near unexpected token `$'{\r''
jdoodle.sh: line 3: `hello_world () {
'
And this
#!/bin/bash
f() { $branchName = "branch" echo $branchName}; f
hello_world
I'm using this tool:
https://www.jdoodle.com/test-bash-shell-script-online/
Why can't I write a simple bash script?
you are missing a ; after your echo command. i just ran this on git bash, recieved the same error then looked at some documentation. After adding the semicolon it functioned perfectly.
function hello { echo 'Hello World!'; }
hello
For example:
Bash-Prog-Intro-HOWTO
function foo() {}
I make search queries in info bash and look in releted chapters of POSIX for function keyword but nothing found.
What is function keyword used in some bash scripts? Is that some deprecated syntax?
The function keyword is optional when defining a function in Bash, as documented in the manual:
Functions are declared using this syntax:
name () compound-command [ redirections ]
or
function name [()] compound-command [ redirections ]
The first form of the syntax is generally preferred because it's compatible with Bourne/Korn/POSIX scripts and so more portable.
That said, sometimes you might want to use the function keyword to prevent Bash aliases from colliding with your function's name. Consider this example:
$ alias foo="echo hi"
$ foo() { :; }
bash: syntax error near unexpected token `('
Here, 'foo' is replaced by the text of the alias of the same name because it's the first word of the command. With function the alias is not expanded:
$ function foo() { :; }
The function keyword is necessary in rare cases when the function name is also an alias. Without it, Bash expands the alias before parsing the function definition -- probably not what you want:
alias mycd=cd
mycd() { cd; ls; } # Alias expansion turns this into cd() { cd; ls; }
mycd # Fails. bash: mycd: command not found
cd # Uh oh, infinite recursion.
With the function keyword, things work as intended:
alias mycd=cd
function mycd() { cd; ls; } # Defines a function named mycd, as expected.
cd # OK, goes to $HOME.
mycd # OK, goes to $HOME.
\mycd # OK, goes to $HOME, lists directory contents.
The reserved word function is optional. See the section 'Shell Function Definitions' in the bash man page.
I'm new at bash script writing and I have this error. I have looked everywhere to find an answer with no success. What is wrong with this script?
#!/bin/bash
exec >> /Users/k_herriage/bin/post-gererate.out 2>&1
date
set -x
mynewfile="~/bin/convert_tst.txt"
myfile=fopen($mynewfile,'w+' );
#echo $myfile
fwrite($myfile, "testing");
fclose($myfile);
exit (0)
line 7: syntax error near unexpected token `('
line 7:`myfile = fopen ( '~/bin/convert_tst.txt','w' );'
Few points:
Calling a function in bash does not require parens, it is syntactically equivalent to a command:
do_something arg1 arg2 arg3
There is no need to do open-append-close sequence in bash, it is perfectly doable with a single command:
echo "testing" >> $mynewfile; ##
>> means "append", where if it was >, it would mean "overwrite" or "discard content". (Both will create the file if it didn't exist.)
Am I constructing this properly? I can't figure how to debug this.
Basically, I'm trying to pass in a text file (login.info):
foo
bar
To this shell script to be used as parameters like so:
#/usr/bin/ksh
#Base dir
BASEDIR=/home/scripts
export BASEDIR;
#Key
KEY=$BASEDIR/login.info
export KEY;
IFS="
"
arr=( $(<$KEY) )
echo "username=${arr[0]} password=${arr[1]}"
Getting this error:
./tst.sh[12]: Syntax error at line 12 : `(' is not expected.
It seems like your version of ksh doesn't understand (...) in array assignment. Maybe this will work better:
set -A arr $(cat $KEY)