LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 10-25-2020, 08:06 AM   #1
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Question Bash script - bad substitution


Hi Folks, This is syntax problem not been able to ddg it so here goes:
Code:
X=$0        # save $0 in a local variable
Y=${0##*/}  # remove dir part
Z=${Y%.*}   # remove extension - if you want to do so
echo "X=\$0:$X  Y=\${0##*/} :$Y   Z=\${Y%.*}:$Z"
and you run it it gives this:
Code:
X=$0:./DVDOpts.sh  Y=${0##*/} :DVDOpts.sh   Z=${Y%.*}:DVDOpts
${0##*/}:DVDOpts.sh
How can I avoid using the intermediate variable Y?
I tried this:
Code:
echo "\${0##*/}:${0##*/} ${{0##*/}%.*} "
which gave the ereror message:
Code:
./DVDOpts.sh: line 21: \${0##*/}:${0##*/} ${{0##*/}%.*} : bad substitution
 
Old 10-25-2020, 08:16 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,320
Blog Entries: 3

Rep: Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725Reputation: 3725
How much of a requirement is Bash? It won't allow nesting but nested parameter expansion works fine in Zsh:

Code:
#!/bin/zsh

X=$0        # save $0 in a local variable
Y=${0##*/}  # remove dir part
Z=${Y%.*}   # remove extension - if you want to do so
echo "X=\$0:$X  Y=\${0##*/} :$Y   Z=\${Y%.*}:$Z"

echo "${${0##*/}%.*}"
 
1 members found this post helpful.
Old 10-25-2020, 08:26 AM   #3
GPGAgent
Senior Member
 
Registered: Oct 2018
Location: Surrey UK
Distribution: Mint 20 xfce 64bit
Posts: 1,026

Original Poster
Blog Entries: 3

Rep: Reputation: 133Reputation: 133
Cheers Turbo....

I don't know Zsh but by trial and error I found this:
Code:
echo $(basename ${0%.*})
which gives me just the script name - no path, no extension - perfect:
Code:
dev$ ./DVDOpts.sh -t T
DVDOpts
I'll mark this as solved in a while, just in case there's another way....

Last edited by GPGAgent; 10-25-2020 at 08:44 AM.
 
Old 10-25-2020, 11:51 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,895

Rep: Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317Reputation: 7317
basename will invoke an additional shell, and that will solve this issue. As it was mentioned you cannot nest (or embed) parameter expansion into each other (in bash). Using built-ins is much faster, and in your case that will be a two-step process.
From the other hand:
Code:
#instead of
echo $(basename ${0%.*})
# you can simply write:
basename ${0%.*}
(which will still use a new shell).
 
Old 10-25-2020, 01:31 PM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,804

Rep: Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203Reputation: 1203
Yes,
Code:
bn=$(basename ${0%.*})
denotes a fork+exec (or even a fork+fork+exec).
While
Code:
x=${0%.*}; bn=${x##*/}
is plain inline.
 
Old 10-25-2020, 02:47 PM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
Once again I'm replying to one of your already SOLVED threads...
Quote:
Originally Posted by GPGAgent View Post
How can I avoid using the intermediate variable Y?
Code:
X=${0##*/}  # save $0 in a local variable, remove dir part
X=${X%.*}   # remove extension - if you want to do so
 
  


Reply



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
[SOLVED] Shell Script: "bash: Bad Substitution - Script for remove "." " thiagofw Programming 14 12-09-2016 10:04 PM
LXer: Command Substitution in a Bash Shell Script LXer Syndicated Linux News 0 02-03-2011 07:30 PM
mkdir in bash script - command substitution casperdaghost Linux - Newbie 3 06-26-2010 05:10 AM
bash script command substitution and quoting brian4xp Linux - Software 8 02-05-2008 11:43 AM
Varible substitution in Bash script RonV Linux - Newbie 2 06-04-2006 12:11 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 07:19 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