Results 1 to 5 of 5

Thread: Generate Random Chars in Bash ?

  1. #1
    Senior Member eawedat's Avatar
    Join Date
    May 2008
    Location
    Palestine
    Posts
    320
    Thanks
    4
    Thanked 3 Times in 3 Posts
    Rep Power
    6

    Default Generate Random Chars in Bash ?

    hey all,,

    Is there a way to generate random characters: a-z / A-Z
    in bash?

    WITHOUT using an array of chars..
    ch=(a b c d ... )


    thanks a lot.
    Be to be and not to pee

  2. #2
    Member Shell_Life's Avatar
    Join Date
    May 2011
    Location
    Bahia, Brazil
    Posts
    99
    Thanks
    0
    Thanked 11 Times in 11 Posts
    Rep Power
    4

    Default

    The following code will give you a number between 01 and 52:
    Code:
     
    typeset -R2 mR=99
    until [[ ${mR} < 53 ]]; do
      mR=${RANDOM}
    done
    echo "mR <$mR>"
    Then you can use it to index your array of 52 elements (a-zA-Z).

  3. #3
    Senior Member eawedat's Avatar
    Join Date
    May 2008
    Location
    Palestine
    Posts
    320
    Thanks
    4
    Thanked 3 Times in 3 Posts
    Rep Power
    6

    Smile

    so still am I in need of array's char?
    Be to be and not to pee

  4. #4
    Senior Member
    Join Date
    Jun 2007
    Location
    Hyderabad, AP, India
    Posts
    806
    Thanks
    44
    Thanked 55 Times in 48 Posts
    Rep Power
    12

    Default

    use below code very much cautiously

    Code:
    < /dev/urandom tr -dc A-Za-z0-9_
    This will generate chars with infinity length, if you want to cut down the length with head command as shown below

    Code:
    < /dev/urandom tr -dc A-Za-z0-9_ | head -c8
    how about if you want to generate only numbers.. you can use inbuilt variable for this..

    Code:
    echo $RANDOM
    Last edited by kumarat9pm; 20th August 2011 at 01:20 PM.
    Thanks,
    Surendra Kumar Anne
    Linux: Fast, friendly, flexible and .... free!
    Support Open source.
    http://www.linuxnix.com

  5. #5
    Senior Member eawedat's Avatar
    Join Date
    May 2008
    Location
    Palestine
    Posts
    320
    Thanks
    4
    Thanked 3 Times in 3 Posts
    Rep Power
    6

    Default

    I thought for example

    rand numbers 65-90
    97-122
    then
    convert decimal to char!!
    Be to be and not to pee

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Command To Generate Random Password String
    By jaysunn in forum Shell scripting
    Replies: 9
    Last Post: 27th June 2011, 07:24 AM
  2. random substitution with awk
    By neppolo in forum Shell scripting
    Replies: 1
    Last Post: 21st May 2011, 01:25 AM
  3. Random Number with /bin/sh
    By pacghost in forum Shell scripting
    Replies: 2
    Last Post: 6th May 2010, 08:31 AM
  4. [Solved] Bash: Random data in arrays
    By pacghost in forum Shell scripting
    Replies: 8
    Last Post: 23rd April 2010, 05:27 AM
  5. Replies: 2
    Last Post: 7th October 2009, 04:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •