Ask Your Question
0

Is it possible to merge two parameter expansions into one command in Bash?

asked 2023-02-25 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-02-09 10:00:00 +0000

lalupa gravatar image

Yes, you can merge two parameter expansions into one command in Bash. For example, you can use the "${parameter:-word}" and "${parameter%word}" expansions together to remove a suffix from a variable and replace it with a different value, all in one command:

filename="example.txt"
new_filename="${filename%.*}.pdf"
echo "Converting ${filename} to ${new_filename}..."

In this example, the first expansion ${filename%.*} removes the suffix ".txt" from the variable filename, and the second expansion ${new_filename:-.pdf} adds the new suffix ".pdf" to the result. The output of the command would be:

Converting example.txt to example.pdf...
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-02-25 11:00:00 +0000

Seen: 10 times

Last updated: Feb 09 '22