Work the Shell - Writing a Shell Game

Bash a little time away with Blackjack.

We've spent the last three columns talking about the basic nuts and bolts of shell script programming, so I think it's time to start digging into a real shell script, and build something interesting and useful. Well, interesting, at least!

What I would like to do—and up front I admit that this might be a crazy hard problem for a shell script—is to try to write a rudimentary Blackjack game. It's simple enough that it should be manageable, but it's hard enough that we'll really have to flex our scripting muscle to get everything working. Needless to say, it won't have a fancy graphical interface!

Onward to Vegas

We'll go into the specific rules of Blackjack as needed, but for now all you need to know about Blackjack is that each player gets two cards from a deck of standard playing cards, and that players can then request additional cards, trying to get their point total as close to 21 as possible, without going over that value. All face cards are worth 10 points each, and an Ace is worth 1 or 11, depending on how the player wants it to count.

The first challenge is to create a virtual deck of cards, but this is easier than you might think, because it can be represented simply by an array of 52 elements, with the first 13 representing one suit, the second 13 a second suit and so forth. So, card #37 might be a Jack of Hearts, for example.

It turns out that shell scripts can use arrays, so let's start by creating a 52-element array and populating it with the values 1–52:

while [ $card -lt 53 ]
  card=$(( $card + 1 ))

If you're used to Perl, you might be thinking that a for loop would be a more logical choice for this sort of task, but for loops in shell scripts lack the ability to step through a range of values. Arrays in the Bourne Shell are easy to work with: simply specify a reference index and the array will be grown to that size dynamically.

Now we have a representation of a deck of cards, but it's perfectly sorted, so the next step is to write some code that will shuffle the deck. This proves to be a bit more tricky, as you might expect!

The basic idea is that we'll randomly pick a number between 1 and 52, and then see if its card is available or not. So the initial deck we created that's sorted is used as the source for the shuffled deck, which will actually end up in a new array. Here's the basic piece of code for the random card selection:

while [ $errcount -lt $threshold ]
   randomcard=$(( ( $RANDOM % 52 ) + 1 ))
   errcount=$(( $errcount + 1 ))

   if [ ${deck[$randomcard]} -ne 0 ] ; then
     deck[$picked]=0         # picked, remove it
     return $picked

There's a lot to see here, but let's talk about the basic logic first: although we're going to pick a card randomly between 1 and 52, and then see if it has already been picked, we also need to make sure we don't end up trapped in an infinite loop because of a mediocre random number function. That's managed by keeping track of the number of guesses you have to make with the variable errcount. The threshold can be adjusted to allow more or fewer guesses for each card. I have it set to 10 as a default value.

You can see that working with arrays makes variable references quite a bit more tricky. Setting the value isn't too bad, as shown earlier, but referencing the array requires the addition of curly braces, so the reference to ${deck[$randomcard]} is to the randomcard slot in the array deck.

Otherwise, don't let all the notation distract you as this is a fairly straightforward loop. Try threshold times to pick a card randomly out of the array deck that hasn't already been chosen (for example, had its value set to zero rather than the initialized value).

The other interesting piece of this code block is the RANDOM variable. Every time you reference $RANDOM, you get a different number between zero and MAXINT (a very large integer value), automatically, without having to initialize anything or do any special work. Try it yourself by typing echo $RANDOM at the Bourne Again Shell command prompt.

This isn't the full code segment, because we also need to have a fall-through, a block of code that is used when the random guesses don't produce a desired card and we instead need to step through the array deck linearly to find one that's available. Typically, it'd be used only at the very end of the shuffle when there are only a few cards left. This code looks like:


while [ ${deck[$randomcard]} -eq 0 ]
   randomcard=$(( $randomcard + 1 ))

deck[$picked]=0             # picked, remove it
return $picked

This should be even easier to read now that you're becoming familiar with arrays.

I'm going to stop here for this month, and we'll pick up the card shuffling task again next month, including an explanation of how to make it a shell function and utilize it in the main game script itself. Stay tuned!

Dave Taylor is a 25-year veteran of UNIX, creator of The Elm Mail System and most recently author of both the best-selling Wicked Cool Shell Scripts and Teach Yourself Unix in 24 Hours, among his 16 technical books. His main Web site is at


Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

while or for ?

magnus.therning's picture

I can see how you'd like to use a while loop in your code, after all it's about shell scripting. However I think that in order to really unleash the full power of the shell one needs to know about the tools that are available. You missed an opportunity to introduce a useful tool in this column--seq.

You're while loop could have been written as a for loop using seq:

for card in `seq 52`; do

Why not use for?

Anton Olsen's picture

Why not use a for loop to increment the numbers?

for ((card=1;card<=52;card++));do

Free Dummies Books
Continuous Engineering


  • What continuous engineering is
  • How to continuously improve complex product designs
  • How to anticipate and respond to markets and clients
  • How to get the most out of your engineering resources

Get your free book now

Sponsored by IBM

Free Dummies Books
Service Virtualization

Learn to:

  • Define service virtualization
  • Select the most beneficial services to virtualize
  • Improve your traditional approach to testing
  • Deliver higher-quality software faster

Get your free book now

Sponsored by IBM