This might sound pretty weird, but I know how to set the vertical cursor position in Bash like this:
echo -e "\e[12H"
This moves the cursor to the 12th line (starting with 1).
So how do I get the cursor position (line number) using linux bash? It would be helpful if I could simply store this value in a variable so I can calculate with it.
EDIT:
This is the error I get:
$ sh rowcol.sh
-en
read: 9: Illegal option -d
test.sh: 12: Bad substitution
Best Answer
I was able to use some of the examples from the same article on SO, titled: How to get the cursor position in bash?. I'm posting this here just to show that they work and that the contents of solutions is actually on U&L as well.
Bash solutions
From inside a script
NOTE: I changed the output slightly!
Example
Interactive shell
This command chain worked for getting the row and column positions of the cursor:
Example
NOTE: This method doesn't appear to be usable from any type of script. Even simple commands in an interactive terminal didn't work for me. For example:
just hangs indefinitely.
dash/sh solutions
From inside a script
This solution is for Ubuntu/Debian systems that come stock with
dash
, which is POSIX compliant. Because of this, theread
command doesn't support the-d
switch among other differences.To get around this there is this solution which uses a
sleep 1
in place of the-d
switch. This isn't ideal but offers at least a working solution.Example
Interactive shell
I couldn't find a workable solution that worked for just
sh
in an interactive shell.