Bash - check the first letter of a var

Posted on 2007-04-06
Last Modified: 2010-05-18
while [ -z $cnickname ] || echo ${cnickname:0:1} | grep [:digit:];
if echo ${cnickname:0:1} | grep [:digit:]; then

When i execute this it says: Syntax error: Bad substitution, any ideea whats wrong ?
The script should take the virst letter of $cnickname and see if its a digit, and if it is do something
Question by:rares_dumitrescu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 84

Expert Comment

ID: 18863049
Are you sure you are running it in bash and not in sh?

Author Comment

ID: 18863093
you're right i was using /bin/sh, i changed it to #!/usr/local/bin/bash .. now it works but the if, or while does not turn on at echo ${cnickname:0:1} | grep [:digit:]; when i put something like '3blah'

Author Comment

ID: 18863113
i know i might sound very stupid, but, why didn't it worked with /bin/sh and it works with /bin/bash ?
whats the difference ? (i am making some scripts on a server, like getegg getpsy etc (for irc) using dialog menu)
LVL 84

Accepted Solution

ozo earned 500 total points
ID: 18863190
they are different shells, see
man sh
man bash
you might try grep '[0-9]'
grep '[[:digit:]]'

Featured Post

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!

Question has a verified solution.

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

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 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