0

I am messing around in bash to try and create a custom PS1 but I'm running into an error when I try to print the absolute path of the directory I am in. I have a directory $FIRMWARE which is a symlink. I want to display the resolved symlink in the PS1. I have the following code stripped of other info:

export FIRMWARE="/path/to/firmware"

firmware_link() {
    local A
    A="$(readlink ${FIRMWARE})"
    echo $A 
}

If I call this function I get

> $(firmware_link)
bash: /path/to/firmware: Is a directory

I tried following this post but the recommendations for quotes didn't solve my issue. I get a feeling this is a formatting error or a fundamental misunderstanding of how bash works.

1 Answer 1

0

By saying:

$(firmware_link)

at a bash prompt. you are telling bash to execute your function, and then process the output from that function as a shell command. That general technique is called command substitution, and in your example code, creates the error that you note.

Instead, try simply invoking the function:

firmware_link

The expected output would be:

/path/to/firmware

To see how the problem is arising, manually enter that directory name at a bash prompt:

/path/to/firmware
-bash: /path/to/firmware: Is a directory

You may spot the error in your code more readily if you were to simplify it slightly:

export FIRMWARE="/path/to/firmware"

firmware_link() {
    readlink "${FIRMWARE}"
}

This makes it more apparent that the output from function firmware_link is going to be a directory name, not a valid bash command.

1
  • Ah ok, I figured it was some misunderstanding with bash return. Thank you!
    – potapeno
    Jan 10, 2022 at 22:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .