I'm still learning unix and I need to know if there is a way to perform the following actions.

Is there any way I can echo a read variable, and in the process, dissect it letter by letter, and assign specific commands to every letter? For example, if the regular echoed output would be "cat", how would I make c, a, and t execute different commands? Is this possible?

I have attempted various methods using fold to split the letters up into their own lines. I just need a way to make the letters equal a command.

It would pretty much look like this:

read variable
echo $variable | fold -w 1

Am I on the right track?

closed as unclear what you're asking by Wildcard, Anthon, jimmij, Archemar, Jakuje Apr 3 '16 at 9:47

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    You may want to learn about option flags. Or else possibly what you want is command1;command2;command3. Piping output to fold won't run commands no matter what, so it's hard to tell what problem you're really trying to solve here. Could you edit your question to add more detail? – Wildcard Apr 3 '16 at 7:28
  • I'm not yet well-versed enough to really elaborate on it at the level you would need me to. I'm trying to make a Morse code translator. What I'm trying to do is create a shell script that can take user input, break down the words by letter, and have the individual letter execute the command to play a wav file of the corresponding morse code. I don't need a full explanation, but just a concept to look into or any good resources you may know. I've seen the code for a version of this project that someone else made, but I don't understand it just yet - I'm too new to this to understand it yet. – nosferatwo Apr 3 '16 at 7:39
  • 1
    Ah, yes, I recall your other question about morse code. Honestly, you're just plain using the wrong tool. Shell script is for orchestrating existing tools to make them cooperate on a given task. For a whole new special-purpose tool, shell scripting is the wrong language. (Not to say you can't do it, but (a) it will be awfully complicated and (b) it won't teach you about what shell scripting is for, nor why you would ever write something in shell.) – Wildcard Apr 3 '16 at 7:44
  • 1
    See unix.stackexchange.com/a/169765/135943 for a further description of what shell scripting is for. – Wildcard Apr 3 '16 at 7:47
  • 1
    Thank you for the explanation and the resource. I was looking for either an explanation of how to do it, or a "this isn't very practical, and here is why." I made a very basic text to morse code translator using sed as my first script project (just to get used to vi and scripts in general) and I wanted to see just how far I could take it. Perhaps one day I'll be able to take that awfully complicated path you mentioned and make it work. But for now, I appreciate the advice. Thanks! – nosferatwo Apr 3 '16 at 7:53

In bash 4, you could define the letter/command mapping in an associative array and loop over all letters as follows:


# List of commands
declare -A cmd=(

# Split entry into letters and execute assigned commands
read variable
for f in `echo $variable | fold -w 1`; do

Not the answer you're looking for? Browse other questions tagged or ask your own question.