Unix update variable by position

Posted on 2009-02-09
Medium Priority
Last Modified: 2013-12-06
Im new to Unix, as part of my learning im creating a hangman game using bash. I am having a problem showing what letters a user has guessed correctly.
So far I have 4 variables $word - that is the word a user is trying to guess, $hidden - that is the same as $word but replaces each letter with a -,  $input_letter that is the current letter guess, and $position which is where the letter occurs in word.
eg $word = join
$hidden = ----
$input_letter = i
$position = 3

I want to replace 1 character in $hidden with $input_letter at $position. I also have to do this without the use of sed or awk.

Any help much appreciated

(unsure of which zone this would come under)
Question by:ukgen
LVL 14

Expert Comment

ID: 23593032
${<Variable Name>:<Offset>[:Length]} eg ${hidden:$position:1}
LVL 46

Expert Comment

by:Kent Olsen
ID: 23593053
Hi ukgen,

"Without the use of sed or awk" suggests that this is a class assignment and you're being handicapped from using the tools specifically designed for this kind of work. :(

Modern bash scripting allows you to access subtrings of a variable.

  echo ${Var1:1}
  echo ${var1:4:1}

It's very much like the substr() function in many modern langauges, expect position is relative 0.

There's a decent write-up on it here:


Good Luck,
LVL 85

Expert Comment

ID: 23593633
in bash, its probably easier to use
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 23594043
sorry everyone thanks for the input

Im still a bit confused, so if im trying to get a variable $hidden that would look like "= --i-"
so the i replaces the third character
i cant see how to use any of the replies that updates $hidden at the stated position with the input_letter
I cant test it at the moment, sorry to be a pain.
Thanks again

LVL 85

Accepted Solution

ozo earned 2000 total points
ID: 23594109
echo $hidden

Author Comment

ID: 23615075
thanks everyone got it working perfect



Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Suggested Courses
Course of the Month15 days, 21 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question