How to pass variables between bash scripts and Matlab… here is a solution to the problem.

## How to pass variables between bash scripts and Matlab

For example, in Matlab, I have a string `x = 'foo'`

so I want to use it in a bash script. Or I have x `=$'foo'`

in the bash script so I want to use `x`

in Matlab. Is there a way to do this?

### Solution

I can’t tell you about matlab, but in Octave everything is simple.

For example, to pass a bash variable to octave, all you need to do is this:

```
Xmax=10; echo "x=[1:$Xmax]; sin(x)" | octave -q
ans =
Columns 1 through 8:
0.84147 0.90930 0.14112 -0.75680 -0.95892 -0.27942 0.65699 0.98936
Columns 9 and 10:
0.41212 -0.54402
```

And vice versa:

```
Xmax=$(echo "fprintf(1, '%g', max(sin([1:5])))" | octave -q); echo $Xmax
0.909297
```