The title remains
Please tell me how to check the type of the value in the variable.
Is there anything that returns int for int type and string for string?
Even if you look for it, it is difficult to find it.
Thank you.
-
Answer # 1
-
Answer # 2
If you want to check integers, strings, and arrays using the
declare
andtypeset
commands in Bash, use-p
option of the>declarecommand.# Declaring variable i as an integer declare -i i = 1 # increment i and display i = i + 1 echo $i # =>2 # check how it was declared with declare -p declare -p i # declare -i i # Is displayed, so use this
In zsh, you can check the variable type as
${(t) foo}
.declare -i i = 1 echo ${(t) i} # integer # Is displayed s = hoge echo ${(t) s} # scalar # Is displayed
If you want to be POSIX compliant, there's no type of variable in the first place. If you want to check if the contents of a variable are something like an integer, use
grep
etc.
I thinki = 0 if echo "$i" | head -n1 | grep '^ [0-9] \ + $'>/dev/null;then echo "integer" else echo "not integer" fi
is good.
-
Answer # 3
See the
man bash
typeset
declare
section of the built-in command for the attributes of the variable.
Do you want to judge whether it's a string consisting only of numbers or a string that contains other than numbers?
if test -n "$A" -a -z "${A // [0-9] /}" then echo only numbers else echo Non-numeric or empty fi
(If you delete all the numbers and are empty, say that the original string was only numbers.)
Related articles
- i want to execute the shell script file (sh) on my pc and check the contents
- i want to fill a shell variable in awk in a bash script
- sed - [shell script] variable, add character string to specific position in character string
- while - [shell script] how to convert "\ n" in a variable
- i wrote a shell script like the one below, but the variables are not reflected properly when i crontab
- Expect-free implementation of shell script
- SQL keyword script check regular expression method
- How to use shell script strings and arrays
- Shell script for calendar screen control
- Shell script refreshes data from the Oracle server to the ftp server at regular intervals
- Super detailed 5 shell script examples to share (worth collecting)
- How to automatically generate a vue file through a shell script
- Java remote call shell script and get output information [recommended]
- Shell script combat DNS master-slave synchronization script example
- A small shell script accurately counts the number of rows in each table of Mysql
- Usage of awk instruction in shell script
- Shell general script startup jar (microservice) under Linux
- Conditional test of shell script and use of if conditional statement
- Write a shell script to boot automatically on ubuntu (recommended)
- Shell script to monitor system resources and alert via SMS
- python - you may need to restart the kernel to use updated packages error
- php - coincheck api authentication doesn't work
- php - i would like to introduce the coincheck api so that i can make payments with bitcoin on my ec site
- [php] i want to get account information using coincheck api
- the emulator process for avd pixel_2_api_29 was killed occurred when the android studio emulator was started, so i would like to
- javascript - how to check if an element exists in puppeteer
- dart - flutter: the instance member'stars' can't be accessed in an initializer error
- sh - 'apt-get' is not recognized as an internal or external command, operable program or batch file
- i want to call a child component method from a parent in vuejs
- python 3x - typeerror: 'method' object is not subscriptable
Anything that has been extended with Bash, etc.,nois the data type for shell scripts considered within the scope of
#!/bin/sh
. Everything is a string.expr
is in command format, but what you receive is anumber string, not the number itself.Check whether the given data is a numerical value with a regular expression, or check if it can be calculated by throwing to
expr
orbc
, etc. There is a way.