# How to define a variable as a substring of another variable?

**URL:** <https://discuss.circleci.com/t/how-to-define-a-variable-as-a-substring-of-another-variable/29003>\
**Category:** Build Environment\
**Created:** [March 14, 2019, 6:58pm UTC](https://discuss.circleci.com/t/how-to-define-a-variable-as-a-substring-of-another-variable/29003 "2019-03-14T18:58:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![circlecibeginner](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@circlecibeginner](https://discuss.circleci.com/u/circlecibeginner)\
**Post date:** [March 14, 2019, 6:58pm UTC](https://discuss.circleci.com/t/how-to-define-a-variable-as-a-substring-of-another-variable/29003/1 "2019-03-14T18:58:16Z")

</div>

I would like to add a command that uses a substring of another variable.

$MY\_VARIABLE is set to first/second. I want to only use everything after the / slash symbol, and either add this to a new variable, or use the result in another command.

How do I take a substring of that environment variable?

---

<div class="post-metadata">

**Author:** ![halfer](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/halfer/32/9083_2.png) [@halfer](https://discuss.circleci.com/u/halfer)\
**Post date:** [March 14, 2019, 8:46pm UTC](https://discuss.circleci.com/t/how-to-define-a-variable-as-a-substring-of-another-variable/29003/2 "2019-03-14T20:46:43Z")

</div>

It depends on where in the YAML config you are wanting to do this. If it is a `run` step where you can use arbitrary shell commands, I expect you can do substring operations using `awk`.

---

<div class="post-metadata">

**Author:** ![circlecibeginner](https://avatars.discourse-cdn.com/v4/letter/c/ecccb3/32.png) [@circlecibeginner](https://discuss.circleci.com/u/circlecibeginner)\
**Post date:** [March 15, 2019, 8:14pm UTC](https://discuss.circleci.com/t/how-to-define-a-variable-as-a-substring-of-another-variable/29003/3 "2019-03-15T20:14:04Z")

</div>

Thanks for your help.

I tried this, where $MY\_VARIABLE is set to first/second, but I only want the second part after the slash symbol.

> ```
> - run:
> name: Set variable
> command: $VARIABLE=$(echo $MY_VARIABLE | cut -d '/' -f 1)
> 
> ```

However, this returns the result: /bin/sh: =circleci: not found

---

<div class="post-metadata">

**Author:** ![halfer](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/halfer/32/9083_2.png) [@halfer](https://discuss.circleci.com/u/halfer)\
**Post date:** [March 16, 2019, 8:39am UTC](https://discuss.circleci.com/t/how-to-define-a-variable-as-a-substring-of-another-variable/29003/4 "2019-03-16T08:39:57Z")

</div>

Could you try this instead, with backticks?

```
VARIABLE=`echo $MY_VARIABLE | cut -d '/' -f 1`

```

Also, to set a variable, you don’t need the dollar symbol.

---

<div class="post-metadata">

**Author:** ![levlaz](https://sea2.discourse-cdn.com/circleci/user_avatar/discuss.circleci.com/levlaz/32/36978_2.png) [@levlaz](https://discuss.circleci.com/u/levlaz)\
**Post date:** [March 16, 2019, 5:28pm UTC](https://discuss.circleci.com/t/how-to-define-a-variable-as-a-substring-of-another-variable/29003/5 "2019-03-16T17:28:27Z")

</div>

Please note that settings this variable will not persist across steps. Each step runs in a separate shell.

You should use the `$BASH_ENV` approach instead: [https://circleci.com/docs/2.0/env-vars/#using-bash\_env-to-set-environment-variables](https://circleci.com/docs/2.0/env-vars/#using-bash_env-to-set-environment-variables)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [March 26, 2019, 5:28pm UTC](https://discuss.circleci.com/t/how-to-define-a-variable-as-a-substring-of-another-variable/29003/6 "2019-03-26T17:28:29Z")

</div>

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
