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
  • 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

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 (…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

744 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

11 Experts available now in Live!

Get 1:1 Help Now