2

I want to display a numbered menu from a shell script (#!/bin/sh). Currently I use

echo "choice1 choice2 choice3 choice4 " 

To output the menu. Now I want to add a number prefix to each line so the result will look like:

1) choice1 2) choice2 3) choice3 4) choice4 ... 

How can I do this without having to manually prefix each line with a number? I'm thinking I want to iterate over the variable line by line and concatenate the line number and output it to a new variable.

2 Answers 2

4

If you are using bash, I'd recommend using the select built-in for these kind of tasks.

Here are some examples.

Sign up to request clarification or add additional context in comments.

Comments

2

select is probably the way to go, but you could also do:

echo "$string" | nl -ba -s') ' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.