Unix update variable by position

Posted on 2009-02-09
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

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

    Expert Comment

    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 84

    Expert Comment

    in bash, its probably easier to use

    Author Comment

    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 84

    Accepted Solution

    echo $hidden

    Author Comment

    thanks everyone got it working perfect



    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    tar syntax help 5 134
    nodeip 9 60
    AIX  NFS  question 5 73
    Sparc Solaris hangs without crash dump & no events from messages 3 44
    Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
    Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
    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…
    In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

    746 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

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now