1

code example: my PWD = /aa/bb/cc/dd

dir= `dirname "$(PWD)"`
output = `basename "${dir}"`

dir variable outputs correctly: /aa/bb/cc

desired output variable: cc

error I'm getting: /bin/sh: 1: Syntax error: Unterminated quoted string

I've tried different quotation combinations and other things like these suggested solutions: https://stackoverflow.com/questions/22401091/bash-variable-substitution-vs-dirname-and-basename/22402242 and https://unix.stackexchange.com/questions/253524/dirname-and-basename-vs-parameter-expansion

4
  • Whenever you have a shell script error, a good first step is to cut and paste your code into shellcheck.net and correct the errors (important) and warnings (might be important) that it identifies. If you have trouble understanding its messages, then come here and ask. Commented Sep 26, 2019 at 18:48
  • 2
    There's an edit attempt from another user that tries to be user1094771. Please see I accidentally created two accounts; how do I merge them? Commented Sep 26, 2019 at 19:44
  • 1
    First of all, welcome to Super User! We are always glad to help, but you apparently have two Super User accounts: this one and this one. Please take the time to utilize the following Help Center tutorial and ask the Super User staff to merge your accounts: I accidentally created two accounts; how do I merge them? Commented Sep 26, 2019 at 19:53
  • The edit was rejected, although maybe it shouldn't have been. If it was really you, the author, then don't let this rejection stop you. After you get the accounts merged you will regain the ownership and be able to edit freely. Edit the question again then. Commented Sep 26, 2019 at 21:39

2 Answers 2

0

Below code is working for me :

dir1=`dirname "$PWD"`
output=`basename "$dir1"`
1
  • yes, space is not needed, got copied from above Commented Sep 26, 2019 at 21:14
0

Instead of "$(PWD)", you'll want "${PWD}". The former tries to execute the command PWD, the latter expands to the value of the variable PWD.

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.