Celeb Glow
updates | April 01, 2026

How to echo "some text" and result of function in one line?

I'm starting to learn Bash scripting. And I had a question. The code below is working fine,

Echo "Here is your current directory: "
pwd

but what if I want to have the result of pwd to be written in the same line as explanation string? How to do it? Thanks.

1 Answer

You can use a command substitution

echo "Here is your current directory: $(pwd)"

However you might want to get into the habit of preferring printf over echo

printf 'Here is your current directory: %s\n' "$(pwd)"

Note: you could just tell echo not to include the terminating newline e.g.

echo -n "Here is your current directory: "
pwd

but it's not recommended - see Why is printf better than echo?

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy