LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop
User Name
Password
Linux - Desktop This forum is for the discussion of all Linux Software used in a desktop context.

Notices


Reply
  Search this Thread
Old 07-21-2023, 12:15 PM   #1
PapaOz
LQ Newbie
 
Registered: Jul 2023
Location: New England
Distribution: Mint
Posts: 5

Rep: Reputation: 0
Bash rejects syntax I think should work


I already know what I want to do is subject to criticism about style. <g>.
However, this question is about the bash parser.

I have a trivial little function:

function MyEcho ()
{ set +x; echo $1; set $DASHX; return }
The parser says "syntax error: unexpected end of file"

But
function MyEcho ()
{
set +x
echo $1
set $DASHX
return
}
works fine,

and
if [ 1 == 1 ]
then
{ echo hello; echo world; return }
works fine.

So why is the first use of multiple statements within braces incorrect
while the second is OK?
 
Old 07-21-2023, 12:18 PM   #2
kakistocrat
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
Blog Entries: 4

Rep: Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971
Welcome.

If the closing brace is on the same line as the return, then there needs to be a semicolon to separate them:

Code:
function MyEcho () { set +x; echo $1; set $DASHX; return; }
 
1 members found this post helpful.
Old 07-21-2023, 12:37 PM   #3
PapaOz
LQ Newbie
 
Registered: Jul 2023
Location: New England
Distribution: Mint
Posts: 5

Original Poster
Rep: Reputation: 0
Thank you

Thank you, Turbocapitalist. It works, of course. I thought I tried that before posting, but I guess I didn't.
 
Old 07-21-2023, 12:39 PM   #4
kakistocrat
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,756
Blog Entries: 4

Rep: Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971Reputation: 3971
No problem. There's also shell check which will catch many things too. There is a web interface for it or you can install it locally from your distro's repository.
 
Old 07-21-2023, 03:38 PM   #5
jmccue
Member
 
Registered: Nov 2008
Location: US
Distribution: slackware
Posts: 884
Blog Entries: 1

Rep: Reputation: 417Reputation: 417Reputation: 417Reputation: 417Reputation: 417
Welcome to LQ

FWIW, programming may have been a better place for this, but you got a good answer anyway, so no harm done
 
Old 07-22-2023, 08:43 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 24,345

Rep: Reputation: 7990Reputation: 7990Reputation: 7990Reputation: 7990Reputation: 7990Reputation: 7990Reputation: 7990Reputation: 7990Reputation: 7990Reputation: 7990Reputation: 7990
see man bash:
Quote:
A simple command is ... terminated by a control operator.
Code:
control operator
    A token that performs a control function. It is one of the following symbols:

        ||   &   &&   ;   ;;   (   )   |   |&   <newline>
Based on this you need to have one of these symbols between return and } .
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: EU Rejects Copyright Directive, Four openSUSE Tumbleweed Snapshots Released, DoublePulsar Modified to Work on Windows IoT Systems, Kde LXer Syndicated Linux News 0 07-06-2018 08:42 AM
LXer: IFC Center Rejects MPAA's 'R' Rating On Snowden Documentary, Says It Should Be 'Essential View LXer Syndicated Linux News 0 12-11-2014 05:11 AM
LXer: EU judge rejects think tank involvement in Microsoft appeal LXer Syndicated Linux News 0 12-20-2005 11:03 PM
C++ syntax error before :: token HELP, i cant find the syntax error :( qwijibow Programming 2 12-14-2004 06:09 PM
I have been having a hard time with dualbooting and i think this should work only1gameguru Linux - General 2 12-04-2004 11:37 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Desktop

All times are GMT -5. The time now is 05:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration