I have a code which will use printf and then echo command. As per my understanding echo will by default come in new line, but not happening when it is used after printf. Below is the code:
#!/bin/bash
function login
{
printf "\n%s" "Please enter ${1} password: "
read -s passcode
}
for i in root admin; do login $i; done
echo "just something"
In this code "\n%s" is important as I might be using multiple users in that loop. The output is like below:
[root@host ]# /tmp/test.sh
Please enter root password:
Please enter admin password: just something
[root@host ]#
I know by putting a echo "" after the loop will simply fix and create a new line, but I would like to know why while looping with printf it's working and why not with echo afterwards?