• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 239
  • Last Modified:

Bash - check the first letter of a var

while [ -z $cnickname ] || echo ${cnickname:0:1} | grep [:digit:];
do
if echo ${cnickname:0:1} | grep [:digit:]; then
something

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
0
rares_dumitrescu
Asked:
rares_dumitrescu
  • 2
  • 2
1 Solution
 
ozoCommented:
Are you sure you are running it in bash and not in sh?
0
 
rares_dumitrescuAuthor Commented:
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'
0
 
rares_dumitrescuAuthor Commented:
worked
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)
0
 
ozoCommented:
they are different shells, see
man sh
and
man bash
you might try grep '[0-9]'
or
grep '[[:digit:]]'
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now